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