0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdcoefct.c
0N/A *
0N/A * Copyright (C) 1994-1997, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains the coefficient buffer controller for decompression.
0N/A * This controller is the top level of the JPEG decompressor proper.
0N/A * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
0N/A *
0N/A * In buffered-image mode, this controller is the interface between
0N/A * input-oriented processing and output-oriented processing.
0N/A * Also, the input side (only) is used when reading a file for transcoding.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A/* Block smoothing is only applicable for progressive JPEG, so: */
0N/A#ifndef D_PROGRESSIVE_SUPPORTED
0N/A#undef BLOCK_SMOOTHING_SUPPORTED
0N/A#endif
0N/A
0N/A/* Private buffer controller object */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_d_coef_controller pub; /* public fields */
0N/A
0N/A /* These variables keep track of the current location of the input side. */
0N/A /* cinfo->input_iMCU_row is also used for this. */
0N/A JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
0N/A int MCU_vert_offset; /* counts MCU rows within iMCU row */
0N/A int MCU_rows_per_iMCU_row; /* number of such rows needed */
0N/A
0N/A /* The output side's location is represented by cinfo->output_iMCU_row. */
0N/A
0N/A /* In single-pass modes, it's sufficient to buffer just one MCU.
0N/A * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
0N/A * and let the entropy decoder write into that workspace each time.
0N/A * (On 80x86, the workspace is FAR even though it's not really very big;
0N/A * this is to keep the module interfaces unchanged when a large coefficient
0N/A * buffer is necessary.)
0N/A * In multi-pass modes, this array points to the current MCU's blocks
0N/A * within the virtual arrays; it is used only by the input side.
0N/A */
0N/A JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
0N/A
0N/A#ifdef D_MULTISCAN_FILES_SUPPORTED
0N/A /* In multi-pass modes, we need a virtual block array for each component. */
0N/A jvirt_barray_ptr whole_image[MAX_COMPONENTS];
0N/A#endif
0N/A
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/A /* When doing block smoothing, we latch coefficient Al values here */
0N/A int * coef_bits_latch;
0N/A#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
0N/A#endif
0N/A} my_coef_controller;
0N/A
0N/Atypedef my_coef_controller * my_coef_ptr;
0N/A
0N/A/* Forward declarations */
0N/AMETHODDEF(int) decompress_onepass
0N/A JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
0N/A#ifdef D_MULTISCAN_FILES_SUPPORTED
0N/AMETHODDEF(int) decompress_data
0N/A JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
0N/A#endif
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/ALOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
0N/AMETHODDEF(int) decompress_smooth_data
0N/A JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
0N/A#endif
0N/A
0N/A
0N/ALOCAL(void)
0N/Astart_iMCU_row (j_decompress_ptr cinfo)
0N/A/* Reset within-iMCU-row counters for a new row (input side) */
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A
0N/A /* In an interleaved scan, an MCU row is the same as an iMCU row.
0N/A * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
0N/A * But at the bottom of the image, process only what's left.
0N/A */
0N/A if (cinfo->comps_in_scan > 1) {
0N/A coef->MCU_rows_per_iMCU_row = 1;
0N/A } else {
0N/A if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
0N/A coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
0N/A else
0N/A coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
0N/A }
0N/A
0N/A coef->MCU_ctr = 0;
0N/A coef->MCU_vert_offset = 0;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize for an input processing pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_input_pass (j_decompress_ptr cinfo)
0N/A{
0N/A cinfo->input_iMCU_row = 0;
0N/A start_iMCU_row(cinfo);
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize for an output processing pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_output_pass (j_decompress_ptr cinfo)
0N/A{
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A
0N/A /* If multipass, check to see whether to use block smoothing on this pass */
0N/A if (coef->pub.coef_arrays != NULL) {
0N/A if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
0N/A coef->pub.decompress_data = decompress_smooth_data;
0N/A else
0N/A coef->pub.decompress_data = decompress_data;
0N/A }
0N/A#endif
0N/A cinfo->output_iMCU_row = 0;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Decompress and return some data in the single-pass case.
0N/A * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
0N/A * Input and output must run in lockstep since we have only a one-MCU buffer.
0N/A * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
0N/A *
0N/A * NB: output_buf contains a plane for each component in image,
0N/A * which we index according to the component's SOF position.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Adecompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A JDIMENSION MCU_col_num; /* index of current MCU within row */
0N/A JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
0N/A JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
0N/A int blkn, ci, xindex, yindex, yoffset, useful_width;
0N/A JSAMPARRAY output_ptr;
0N/A JDIMENSION start_col, output_col;
0N/A jpeg_component_info *compptr;
0N/A inverse_DCT_method_ptr inverse_DCT;
0N/A
0N/A /* Loop to process as much as one whole iMCU row */
0N/A for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
0N/A yoffset++) {
0N/A for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
0N/A MCU_col_num++) {
0N/A /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
0N/A jzero_far((void FAR *) coef->MCU_buffer[0],
0N/A (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
0N/A if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
0N/A /* Suspension forced; update state counters and exit */
0N/A coef->MCU_vert_offset = yoffset;
0N/A coef->MCU_ctr = MCU_col_num;
0N/A return JPEG_SUSPENDED;
0N/A }
0N/A /* Determine where data should go in output_buf and do the IDCT thing.
0N/A * We skip dummy blocks at the right and bottom edges (but blkn gets
0N/A * incremented past them!). Note the inner loop relies on having
0N/A * allocated the MCU_buffer[] blocks sequentially.
0N/A */
0N/A blkn = 0; /* index of current DCT block within MCU */
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A /* Don't bother to IDCT an uninteresting component. */
0N/A if (! compptr->component_needed) {
0N/A blkn += compptr->MCU_blocks;
0N/A continue;
0N/A }
0N/A inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
0N/A useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
0N/A : compptr->last_col_width;
0N/A output_ptr = output_buf[compptr->component_index] +
0N/A yoffset * compptr->DCT_scaled_size;
0N/A start_col = MCU_col_num * compptr->MCU_sample_width;
0N/A for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
0N/A if (cinfo->input_iMCU_row < last_iMCU_row ||
0N/A yoffset+yindex < compptr->last_row_height) {
0N/A output_col = start_col;
0N/A for (xindex = 0; xindex < useful_width; xindex++) {
0N/A (*inverse_DCT) (cinfo, compptr,
0N/A (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
0N/A output_ptr, output_col);
0N/A output_col += compptr->DCT_scaled_size;
0N/A }
0N/A }
0N/A blkn += compptr->MCU_width;
0N/A output_ptr += compptr->DCT_scaled_size;
0N/A }
0N/A }
0N/A }
0N/A /* Completed an MCU row, but perhaps not an iMCU row */
0N/A coef->MCU_ctr = 0;
0N/A }
0N/A /* Completed the iMCU row, advance counters for next one */
0N/A cinfo->output_iMCU_row++;
0N/A if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
0N/A start_iMCU_row(cinfo);
0N/A return JPEG_ROW_COMPLETED;
0N/A }
0N/A /* Completed the scan */
0N/A (*cinfo->inputctl->finish_input_pass) (cinfo);
0N/A return JPEG_SCAN_COMPLETED;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Dummy consume-input routine for single-pass operation.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Adummy_consume_data (j_decompress_ptr cinfo)
0N/A{
0N/A return JPEG_SUSPENDED; /* Always indicate nothing was done */
0N/A}
0N/A
0N/A
0N/A#ifdef D_MULTISCAN_FILES_SUPPORTED
0N/A
0N/A/*
0N/A * Consume input data and store it in the full-image coefficient buffer.
0N/A * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
0N/A * ie, v_samp_factor block rows for each component in the scan.
0N/A * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Aconsume_data (j_decompress_ptr cinfo)
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A JDIMENSION MCU_col_num; /* index of current MCU within row */
0N/A int blkn, ci, xindex, yindex, yoffset;
0N/A JDIMENSION start_col;
0N/A JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
0N/A JBLOCKROW buffer_ptr;
0N/A jpeg_component_info *compptr;
0N/A
0N/A /* Align the virtual buffers for the components used in this scan. */
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A buffer[ci] = (*cinfo->mem->access_virt_barray)
0N/A ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
0N/A cinfo->input_iMCU_row * compptr->v_samp_factor,
0N/A (JDIMENSION) compptr->v_samp_factor, TRUE);
0N/A /* Note: entropy decoder expects buffer to be zeroed,
0N/A * but this is handled automatically by the memory manager
0N/A * because we requested a pre-zeroed array.
0N/A */
0N/A }
0N/A
0N/A /* Loop to process one whole iMCU row */
0N/A for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
0N/A yoffset++) {
0N/A for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
0N/A MCU_col_num++) {
0N/A /* Construct list of pointers to DCT blocks belonging to this MCU */
0N/A blkn = 0; /* index of current DCT block within MCU */
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A start_col = MCU_col_num * compptr->MCU_width;
0N/A for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
0N/A buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
0N/A for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
0N/A coef->MCU_buffer[blkn++] = buffer_ptr++;
0N/A }
0N/A }
0N/A }
0N/A /* Try to fetch the MCU. */
0N/A if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
0N/A /* Suspension forced; update state counters and exit */
0N/A coef->MCU_vert_offset = yoffset;
0N/A coef->MCU_ctr = MCU_col_num;
0N/A return JPEG_SUSPENDED;
0N/A }
0N/A }
0N/A /* Completed an MCU row, but perhaps not an iMCU row */
0N/A coef->MCU_ctr = 0;
0N/A }
0N/A /* Completed the iMCU row, advance counters for next one */
0N/A if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
0N/A start_iMCU_row(cinfo);
0N/A return JPEG_ROW_COMPLETED;
0N/A }
0N/A /* Completed the scan */
0N/A (*cinfo->inputctl->finish_input_pass) (cinfo);
0N/A return JPEG_SCAN_COMPLETED;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Decompress and return some data in the multi-pass case.
0N/A * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
0N/A * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
0N/A *
0N/A * NB: output_buf contains a plane for each component in image.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Adecompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
0N/A JDIMENSION block_num;
0N/A int ci, block_row, block_rows;
0N/A JBLOCKARRAY buffer;
0N/A JBLOCKROW buffer_ptr;
0N/A JSAMPARRAY output_ptr;
0N/A JDIMENSION output_col;
0N/A jpeg_component_info *compptr;
0N/A inverse_DCT_method_ptr inverse_DCT;
0N/A
0N/A /* Force some input to be done if we are getting ahead of the input. */
0N/A while (cinfo->input_scan_number < cinfo->output_scan_number ||
0N/A (cinfo->input_scan_number == cinfo->output_scan_number &&
0N/A cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
0N/A if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
0N/A return JPEG_SUSPENDED;
0N/A }
0N/A
0N/A /* OK, output from the virtual arrays. */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Don't bother to IDCT an uninteresting component. */
0N/A if (! compptr->component_needed)
0N/A continue;
0N/A /* Align the virtual buffer for this component. */
0N/A buffer = (*cinfo->mem->access_virt_barray)
0N/A ((j_common_ptr) cinfo, coef->whole_image[ci],
0N/A cinfo->output_iMCU_row * compptr->v_samp_factor,
0N/A (JDIMENSION) compptr->v_samp_factor, FALSE);
0N/A /* Count non-dummy DCT block rows in this iMCU row. */
0N/A if (cinfo->output_iMCU_row < last_iMCU_row)
0N/A block_rows = compptr->v_samp_factor;
0N/A else {
0N/A /* NB: can't use last_row_height here; it is input-side-dependent! */
0N/A block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
0N/A if (block_rows == 0) block_rows = compptr->v_samp_factor;
0N/A }
0N/A inverse_DCT = cinfo->idct->inverse_DCT[ci];
0N/A output_ptr = output_buf[ci];
0N/A /* Loop over all DCT blocks to be processed. */
0N/A for (block_row = 0; block_row < block_rows; block_row++) {
0N/A buffer_ptr = buffer[block_row];
0N/A output_col = 0;
0N/A for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
0N/A (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
0N/A output_ptr, output_col);
0N/A buffer_ptr++;
0N/A output_col += compptr->DCT_scaled_size;
0N/A }
0N/A output_ptr += compptr->DCT_scaled_size;
0N/A }
0N/A }
0N/A
0N/A if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
0N/A return JPEG_ROW_COMPLETED;
0N/A return JPEG_SCAN_COMPLETED;
0N/A}
0N/A
0N/A#endif /* D_MULTISCAN_FILES_SUPPORTED */
0N/A
0N/A
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/A
0N/A/*
0N/A * This code applies interblock smoothing as described by section K.8
0N/A * of the JPEG standard: the first 5 AC coefficients are estimated from
0N/A * the DC values of a DCT block and its 8 neighboring blocks.
0N/A * We apply smoothing only for progressive JPEG decoding, and only if
0N/A * the coefficients it can estimate are not yet known to full precision.
0N/A */
0N/A
0N/A/* Natural-order array positions of the first 5 zigzag-order coefficients */
0N/A#define Q01_POS 1
0N/A#define Q10_POS 8
0N/A#define Q20_POS 16
0N/A#define Q11_POS 9
0N/A#define Q02_POS 2
0N/A
0N/A/*
0N/A * Determine whether block smoothing is applicable and safe.
0N/A * We also latch the current states of the coef_bits[] entries for the
0N/A * AC coefficients; otherwise, if the input side of the decompressor
0N/A * advances into a new scan, we might think the coefficients are known
0N/A * more accurately than they really are.
0N/A */
0N/A
0N/ALOCAL(boolean)
0N/Asmoothing_ok (j_decompress_ptr cinfo)
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A boolean smoothing_useful = FALSE;
0N/A int ci, coefi;
0N/A jpeg_component_info *compptr;
0N/A JQUANT_TBL * qtable;
0N/A int * coef_bits;
0N/A int * coef_bits_latch;
0N/A
0N/A if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
0N/A return FALSE;
0N/A
0N/A /* Allocate latch area if not already done */
0N/A if (coef->coef_bits_latch == NULL)
0N/A coef->coef_bits_latch = (int *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A cinfo->num_components *
0N/A (SAVED_COEFS * SIZEOF(int)));
0N/A coef_bits_latch = coef->coef_bits_latch;
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* All components' quantization values must already be latched. */
0N/A if ((qtable = compptr->quant_table) == NULL)
0N/A return FALSE;
0N/A /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
0N/A if (qtable->quantval[0] == 0 ||
0N/A qtable->quantval[Q01_POS] == 0 ||
0N/A qtable->quantval[Q10_POS] == 0 ||
0N/A qtable->quantval[Q20_POS] == 0 ||
0N/A qtable->quantval[Q11_POS] == 0 ||
0N/A qtable->quantval[Q02_POS] == 0)
0N/A return FALSE;
0N/A /* DC values must be at least partly known for all components. */
0N/A coef_bits = cinfo->coef_bits[ci];
0N/A if (coef_bits[0] < 0)
0N/A return FALSE;
0N/A /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
0N/A for (coefi = 1; coefi <= 5; coefi++) {
0N/A coef_bits_latch[coefi] = coef_bits[coefi];
0N/A if (coef_bits[coefi] != 0)
0N/A smoothing_useful = TRUE;
0N/A }
0N/A coef_bits_latch += SAVED_COEFS;
0N/A }
0N/A
0N/A return smoothing_useful;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Variant of decompress_data for use when doing block smoothing.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Adecompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
0N/A{
0N/A my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
0N/A JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
0N/A JDIMENSION block_num, last_block_column;
0N/A int ci, block_row, block_rows, access_rows;
0N/A JBLOCKARRAY buffer;
0N/A JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
0N/A JSAMPARRAY output_ptr;
0N/A JDIMENSION output_col;
0N/A jpeg_component_info *compptr;
0N/A inverse_DCT_method_ptr inverse_DCT;
0N/A boolean first_row, last_row;
0N/A JBLOCK workspace;
0N/A int *coef_bits;
0N/A JQUANT_TBL *quanttbl;
0N/A INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
0N/A int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
0N/A int Al, pred;
0N/A
0N/A /* Force some input to be done if we are getting ahead of the input. */
0N/A while (cinfo->input_scan_number <= cinfo->output_scan_number &&
0N/A ! cinfo->inputctl->eoi_reached) {
0N/A if (cinfo->input_scan_number == cinfo->output_scan_number) {
0N/A /* If input is working on current scan, we ordinarily want it to
0N/A * have completed the current row. But if input scan is DC,
0N/A * we want it to keep one row ahead so that next block row's DC
0N/A * values are up to date.
0N/A */
0N/A JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
0N/A if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
0N/A break;
0N/A }
0N/A if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
0N/A return JPEG_SUSPENDED;
0N/A }
0N/A
0N/A /* OK, output from the virtual arrays. */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Don't bother to IDCT an uninteresting component. */
0N/A if (! compptr->component_needed)
0N/A continue;
0N/A /* Count non-dummy DCT block rows in this iMCU row. */
0N/A if (cinfo->output_iMCU_row < last_iMCU_row) {
0N/A block_rows = compptr->v_samp_factor;
0N/A access_rows = block_rows * 2; /* this and next iMCU row */
0N/A last_row = FALSE;
0N/A } else {
0N/A /* NB: can't use last_row_height here; it is input-side-dependent! */
0N/A block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
0N/A if (block_rows == 0) block_rows = compptr->v_samp_factor;
0N/A access_rows = block_rows; /* this iMCU row only */
0N/A last_row = TRUE;
0N/A }
0N/A /* Align the virtual buffer for this component. */
0N/A if (cinfo->output_iMCU_row > 0) {
0N/A access_rows += compptr->v_samp_factor; /* prior iMCU row too */
0N/A buffer = (*cinfo->mem->access_virt_barray)
0N/A ((j_common_ptr) cinfo, coef->whole_image[ci],
0N/A (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
0N/A (JDIMENSION) access_rows, FALSE);
0N/A buffer += compptr->v_samp_factor; /* point to current iMCU row */
0N/A first_row = FALSE;
0N/A } else {
0N/A buffer = (*cinfo->mem->access_virt_barray)
0N/A ((j_common_ptr) cinfo, coef->whole_image[ci],
0N/A (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
0N/A first_row = TRUE;
0N/A }
0N/A /* Fetch component-dependent info */
0N/A coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
0N/A quanttbl = compptr->quant_table;
0N/A Q00 = quanttbl->quantval[0];
0N/A Q01 = quanttbl->quantval[Q01_POS];
0N/A Q10 = quanttbl->quantval[Q10_POS];
0N/A Q20 = quanttbl->quantval[Q20_POS];
0N/A Q11 = quanttbl->quantval[Q11_POS];
0N/A Q02 = quanttbl->quantval[Q02_POS];
0N/A inverse_DCT = cinfo->idct->inverse_DCT[ci];
0N/A output_ptr = output_buf[ci];
0N/A /* Loop over all DCT blocks to be processed. */
0N/A for (block_row = 0; block_row < block_rows; block_row++) {
0N/A buffer_ptr = buffer[block_row];
0N/A if (first_row && block_row == 0)
0N/A prev_block_row = buffer_ptr;
0N/A else
0N/A prev_block_row = buffer[block_row-1];
0N/A if (last_row && block_row == block_rows-1)
0N/A next_block_row = buffer_ptr;
0N/A else
0N/A next_block_row = buffer[block_row+1];
0N/A /* We fetch the surrounding DC values using a sliding-register approach.
0N/A * Initialize all nine here so as to do the right thing on narrow pics.
0N/A */
0N/A DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
0N/A DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
0N/A DC7 = DC8 = DC9 = (int) next_block_row[0][0];
0N/A output_col = 0;
0N/A last_block_column = compptr->width_in_blocks - 1;
0N/A for (block_num = 0; block_num <= last_block_column; block_num++) {
0N/A /* Fetch current DCT block into workspace so we can modify it. */
0N/A jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
0N/A /* Update DC values */
0N/A if (block_num < last_block_column) {
0N/A DC3 = (int) prev_block_row[1][0];
0N/A DC6 = (int) buffer_ptr[1][0];
0N/A DC9 = (int) next_block_row[1][0];
0N/A }
0N/A /* Compute coefficient estimates per K.8.
0N/A * An estimate is applied only if coefficient is still zero,
0N/A * and is not known to be fully accurate.
0N/A */
0N/A /* AC01 */
0N/A if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
0N/A num = 36 * Q00 * (DC4 - DC6);
0N/A if (num >= 0) {
0N/A pred = (int) (((Q01<<7) + num) / (Q01<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A } else {
0N/A pred = (int) (((Q01<<7) - num) / (Q01<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A pred = -pred;
0N/A }
0N/A workspace[1] = (JCOEF) pred;
0N/A }
0N/A /* AC10 */
0N/A if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
0N/A num = 36 * Q00 * (DC2 - DC8);
0N/A if (num >= 0) {
0N/A pred = (int) (((Q10<<7) + num) / (Q10<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A } else {
0N/A pred = (int) (((Q10<<7) - num) / (Q10<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A pred = -pred;
0N/A }
0N/A workspace[8] = (JCOEF) pred;
0N/A }
0N/A /* AC20 */
0N/A if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
0N/A num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
0N/A if (num >= 0) {
0N/A pred = (int) (((Q20<<7) + num) / (Q20<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A } else {
0N/A pred = (int) (((Q20<<7) - num) / (Q20<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A pred = -pred;
0N/A }
0N/A workspace[16] = (JCOEF) pred;
0N/A }
0N/A /* AC11 */
0N/A if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
0N/A num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
0N/A if (num >= 0) {
0N/A pred = (int) (((Q11<<7) + num) / (Q11<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A } else {
0N/A pred = (int) (((Q11<<7) - num) / (Q11<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A pred = -pred;
0N/A }
0N/A workspace[9] = (JCOEF) pred;
0N/A }
0N/A /* AC02 */
0N/A if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
0N/A num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
0N/A if (num >= 0) {
0N/A pred = (int) (((Q02<<7) + num) / (Q02<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A } else {
0N/A pred = (int) (((Q02<<7) - num) / (Q02<<8));
0N/A if (Al > 0 && pred >= (1<<Al))
0N/A pred = (1<<Al)-1;
0N/A pred = -pred;
0N/A }
0N/A workspace[2] = (JCOEF) pred;
0N/A }
0N/A /* OK, do the IDCT */
0N/A (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
0N/A output_ptr, output_col);
0N/A /* Advance for next column */
0N/A DC1 = DC2; DC2 = DC3;
0N/A DC4 = DC5; DC5 = DC6;
0N/A DC7 = DC8; DC8 = DC9;
0N/A buffer_ptr++, prev_block_row++, next_block_row++;
0N/A output_col += compptr->DCT_scaled_size;
0N/A }
0N/A output_ptr += compptr->DCT_scaled_size;
0N/A }
0N/A }
0N/A
0N/A if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
0N/A return JPEG_ROW_COMPLETED;
0N/A return JPEG_SCAN_COMPLETED;
0N/A}
0N/A
0N/A#endif /* BLOCK_SMOOTHING_SUPPORTED */
0N/A
0N/A
0N/A/*
0N/A * Initialize coefficient buffer controller.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
0N/A{
0N/A my_coef_ptr coef;
0N/A
0N/A coef = (my_coef_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_coef_controller));
0N/A cinfo->coef = (struct jpeg_d_coef_controller *) coef;
0N/A coef->pub.start_input_pass = start_input_pass;
0N/A coef->pub.start_output_pass = start_output_pass;
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/A coef->coef_bits_latch = NULL;
0N/A#endif
0N/A
0N/A /* Create the coefficient buffer. */
0N/A if (need_full_buffer) {
0N/A#ifdef D_MULTISCAN_FILES_SUPPORTED
0N/A /* Allocate a full-image virtual array for each component, */
0N/A /* padded to a multiple of samp_factor DCT blocks in each direction. */
0N/A /* Note we ask for a pre-zeroed array. */
0N/A int ci, access_rows;
0N/A jpeg_component_info *compptr;
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A access_rows = compptr->v_samp_factor;
0N/A#ifdef BLOCK_SMOOTHING_SUPPORTED
0N/A /* If block smoothing could be used, need a bigger window */
0N/A if (cinfo->progressive_mode)
0N/A access_rows *= 3;
0N/A#endif
0N/A coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
0N/A (JDIMENSION) jround_up((long) compptr->width_in_blocks,
0N/A (long) compptr->h_samp_factor),
0N/A (JDIMENSION) jround_up((long) compptr->height_in_blocks,
0N/A (long) compptr->v_samp_factor),
0N/A (JDIMENSION) access_rows);
0N/A }
0N/A coef->pub.consume_data = consume_data;
0N/A coef->pub.decompress_data = decompress_data;
0N/A coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
0N/A#else
0N/A ERREXIT(cinfo, JERR_NOT_COMPILED);
0N/A#endif
0N/A } else {
0N/A /* We only need a single-MCU buffer. */
0N/A JBLOCKROW buffer;
0N/A int i;
0N/A
0N/A buffer = (JBLOCKROW)
0N/A (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
0N/A for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
0N/A coef->MCU_buffer[i] = buffer + i;
0N/A }
0N/A coef->pub.consume_data = dummy_consume_data;
0N/A coef->pub.decompress_data = decompress_onepass;
0N/A coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
0N/A }
0N/A}