Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdcoefct.c |
| 3 | * |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 1994-1997, Thomas G. Lane. |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 5 | * Copyright (C) 2010, D. R. Commander. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 6 | * This file is part of the Independent JPEG Group's software. |
| 7 | * For conditions of distribution and use, see the accompanying README file. |
| 8 | * |
| 9 | * This file contains the coefficient buffer controller for decompression. |
| 10 | * This controller is the top level of the JPEG decompressor proper. |
| 11 | * The coefficient buffer lies between entropy decoding and inverse-DCT steps. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 12 | * |
| 13 | * In buffered-image mode, this controller is the interface between |
| 14 | * input-oriented processing and output-oriented processing. |
| 15 | * Also, the input side (only) is used when reading a file for transcoding. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #define JPEG_INTERNALS |
| 19 | #include "jinclude.h" |
| 20 | #include "jpeglib.h" |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 21 | #include "jpegcomp.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 22 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 23 | /* Block smoothing is only applicable for progressive JPEG, so: */ |
| 24 | #ifndef D_PROGRESSIVE_SUPPORTED |
| 25 | #undef BLOCK_SMOOTHING_SUPPORTED |
| 26 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 27 | |
| 28 | /* Private buffer controller object */ |
| 29 | |
| 30 | typedef struct { |
| 31 | struct jpeg_d_coef_controller pub; /* public fields */ |
| 32 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 33 | /* These variables keep track of the current location of the input side. */ |
| 34 | /* cinfo->input_iMCU_row is also used for this. */ |
| 35 | JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 36 | int MCU_vert_offset; /* counts MCU rows within iMCU row */ |
| 37 | int MCU_rows_per_iMCU_row; /* number of such rows needed */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 38 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 39 | /* The output side's location is represented by cinfo->output_iMCU_row. */ |
| 40 | |
| 41 | /* In single-pass modes, it's sufficient to buffer just one MCU. |
| 42 | * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 43 | * and let the entropy decoder write into that workspace each time. |
| 44 | * (On 80x86, the workspace is FAR even though it's not really very big; |
| 45 | * this is to keep the module interfaces unchanged when a large coefficient |
| 46 | * buffer is necessary.) |
| 47 | * In multi-pass modes, this array points to the current MCU's blocks |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 48 | * within the virtual arrays; it is used only by the input side. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 49 | */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 50 | JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 51 | |
Pierre Ossman | 35c4719 | 2009-03-09 13:29:37 +0000 | [diff] [blame] | 52 | /* Temporary workspace for one MCU */ |
| 53 | JCOEF * workspace; |
| 54 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 55 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 56 | /* In multi-pass modes, we need a virtual block array for each component. */ |
| 57 | jvirt_barray_ptr whole_image[MAX_COMPONENTS]; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
| 61 | /* When doing block smoothing, we latch coefficient Al values here */ |
| 62 | int * coef_bits_latch; |
| 63 | #define SAVED_COEFS 6 /* we save coef_bits[0..5] */ |
| 64 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 65 | } my_coef_controller; |
| 66 | |
| 67 | typedef my_coef_controller * my_coef_ptr; |
| 68 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 69 | /* Forward declarations */ |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 70 | METHODDEF(int) decompress_onepass |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 71 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
| 72 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 73 | METHODDEF(int) decompress_data |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 74 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 75 | #endif |
| 76 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 77 | LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); |
| 78 | METHODDEF(int) decompress_smooth_data |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 79 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
| 80 | #endif |
| 81 | |
| 82 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 83 | LOCAL(void) |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 84 | start_iMCU_row (j_decompress_ptr cinfo) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 85 | /* Reset within-iMCU-row counters for a new row (input side) */ |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 86 | { |
| 87 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 88 | |
| 89 | /* In an interleaved scan, an MCU row is the same as an iMCU row. |
| 90 | * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. |
| 91 | * But at the bottom of the image, process only what's left. |
| 92 | */ |
| 93 | if (cinfo->comps_in_scan > 1) { |
| 94 | coef->MCU_rows_per_iMCU_row = 1; |
| 95 | } else { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 96 | if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 97 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; |
| 98 | else |
| 99 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; |
| 100 | } |
| 101 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 102 | coef->MCU_ctr = 0; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 103 | coef->MCU_vert_offset = 0; |
| 104 | } |
| 105 | |
| 106 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 107 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 108 | * Initialize for an input processing pass. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 109 | */ |
| 110 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 111 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 112 | start_input_pass (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 113 | { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 114 | cinfo->input_iMCU_row = 0; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 115 | start_iMCU_row(cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
| 119 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 120 | * Initialize for an output processing pass. |
| 121 | */ |
| 122 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 123 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 124 | start_output_pass (j_decompress_ptr cinfo) |
| 125 | { |
| 126 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
| 127 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 128 | |
| 129 | /* If multipass, check to see whether to use block smoothing on this pass */ |
| 130 | if (coef->pub.coef_arrays != NULL) { |
| 131 | if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) |
| 132 | coef->pub.decompress_data = decompress_smooth_data; |
| 133 | else |
| 134 | coef->pub.decompress_data = decompress_data; |
| 135 | } |
| 136 | #endif |
| 137 | cinfo->output_iMCU_row = 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Decompress and return some data in the single-pass case. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 143 | * Always attempts to emit one fully interleaved MCU row ("iMCU" row). |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 144 | * Input and output must run in lockstep since we have only a one-MCU buffer. |
| 145 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 146 | * |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 147 | * NB: output_buf contains a plane for each component in image, |
| 148 | * which we index according to the component's SOF position. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 149 | */ |
| 150 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 151 | METHODDEF(int) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 152 | decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 153 | { |
| 154 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 155 | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
| 156 | JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 157 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
| 158 | int blkn, ci, xindex, yindex, yoffset, useful_width; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 159 | JSAMPARRAY output_ptr; |
| 160 | JDIMENSION start_col, output_col; |
| 161 | jpeg_component_info *compptr; |
| 162 | inverse_DCT_method_ptr inverse_DCT; |
| 163 | |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 164 | /* Loop to process as much as one whole iMCU row */ |
| 165 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
| 166 | yoffset++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 167 | for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 168 | MCU_col_num++) { |
| 169 | /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ |
| 170 | jzero_far((void FAR *) coef->MCU_buffer[0], |
| 171 | (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); |
| 172 | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { |
| 173 | /* Suspension forced; update state counters and exit */ |
| 174 | coef->MCU_vert_offset = yoffset; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 175 | coef->MCU_ctr = MCU_col_num; |
| 176 | return JPEG_SUSPENDED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 177 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 178 | /* Determine where data should go in output_buf and do the IDCT thing. |
| 179 | * We skip dummy blocks at the right and bottom edges (but blkn gets |
| 180 | * incremented past them!). Note the inner loop relies on having |
| 181 | * allocated the MCU_buffer[] blocks sequentially. |
| 182 | */ |
| 183 | blkn = 0; /* index of current DCT block within MCU */ |
| 184 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 185 | compptr = cinfo->cur_comp_info[ci]; |
| 186 | /* Don't bother to IDCT an uninteresting component. */ |
| 187 | if (! compptr->component_needed) { |
| 188 | blkn += compptr->MCU_blocks; |
| 189 | continue; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 190 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 191 | inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; |
| 192 | useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width |
| 193 | : compptr->last_col_width; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 194 | output_ptr = output_buf[compptr->component_index] + |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 195 | yoffset * compptr->_DCT_scaled_size; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 196 | start_col = MCU_col_num * compptr->MCU_sample_width; |
| 197 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 198 | if (cinfo->input_iMCU_row < last_iMCU_row || |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 199 | yoffset+yindex < compptr->last_row_height) { |
| 200 | output_col = start_col; |
| 201 | for (xindex = 0; xindex < useful_width; xindex++) { |
| 202 | (*inverse_DCT) (cinfo, compptr, |
| 203 | (JCOEFPTR) coef->MCU_buffer[blkn+xindex], |
| 204 | output_ptr, output_col); |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 205 | output_col += compptr->_DCT_scaled_size; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | blkn += compptr->MCU_width; |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 209 | output_ptr += compptr->_DCT_scaled_size; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 210 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 213 | /* Completed an MCU row, but perhaps not an iMCU row */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 214 | coef->MCU_ctr = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 215 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 216 | /* Completed the iMCU row, advance counters for next one */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 217 | cinfo->output_iMCU_row++; |
| 218 | if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { |
| 219 | start_iMCU_row(cinfo); |
| 220 | return JPEG_ROW_COMPLETED; |
| 221 | } |
| 222 | /* Completed the scan */ |
| 223 | (*cinfo->inputctl->finish_input_pass) (cinfo); |
| 224 | return JPEG_SCAN_COMPLETED; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* |
| 229 | * Dummy consume-input routine for single-pass operation. |
| 230 | */ |
| 231 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 232 | METHODDEF(int) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 233 | dummy_consume_data (j_decompress_ptr cinfo) |
| 234 | { |
| 235 | return JPEG_SUSPENDED; /* Always indicate nothing was done */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 240 | |
| 241 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 242 | * Consume input data and store it in the full-image coefficient buffer. |
| 243 | * We read as much as one fully interleaved MCU row ("iMCU" row) per call, |
| 244 | * ie, v_samp_factor block rows for each component in the scan. |
| 245 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 246 | */ |
| 247 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 248 | METHODDEF(int) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 249 | consume_data (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 250 | { |
| 251 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 252 | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 253 | int blkn, ci, xindex, yindex, yoffset; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 254 | JDIMENSION start_col; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 255 | JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; |
| 256 | JBLOCKROW buffer_ptr; |
| 257 | jpeg_component_info *compptr; |
| 258 | |
| 259 | /* Align the virtual buffers for the components used in this scan. */ |
| 260 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 261 | compptr = cinfo->cur_comp_info[ci]; |
| 262 | buffer[ci] = (*cinfo->mem->access_virt_barray) |
| 263 | ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 264 | cinfo->input_iMCU_row * compptr->v_samp_factor, |
| 265 | (JDIMENSION) compptr->v_samp_factor, TRUE); |
| 266 | /* Note: entropy decoder expects buffer to be zeroed, |
| 267 | * but this is handled automatically by the memory manager |
| 268 | * because we requested a pre-zeroed array. |
| 269 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 272 | /* Loop to process one whole iMCU row */ |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 273 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
| 274 | yoffset++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 275 | for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 276 | MCU_col_num++) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 277 | /* Construct list of pointers to DCT blocks belonging to this MCU */ |
| 278 | blkn = 0; /* index of current DCT block within MCU */ |
| 279 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 280 | compptr = cinfo->cur_comp_info[ci]; |
| 281 | start_col = MCU_col_num * compptr->MCU_width; |
| 282 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
| 283 | buffer_ptr = buffer[ci][yindex+yoffset] + start_col; |
| 284 | for (xindex = 0; xindex < compptr->MCU_width; xindex++) { |
| 285 | coef->MCU_buffer[blkn++] = buffer_ptr++; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | /* Try to fetch the MCU. */ |
| 290 | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 291 | /* Suspension forced; update state counters and exit */ |
| 292 | coef->MCU_vert_offset = yoffset; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 293 | coef->MCU_ctr = MCU_col_num; |
| 294 | return JPEG_SUSPENDED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 297 | /* Completed an MCU row, but perhaps not an iMCU row */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 298 | coef->MCU_ctr = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 299 | } |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 300 | /* Completed the iMCU row, advance counters for next one */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 301 | if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { |
| 302 | start_iMCU_row(cinfo); |
| 303 | return JPEG_ROW_COMPLETED; |
| 304 | } |
| 305 | /* Completed the scan */ |
| 306 | (*cinfo->inputctl->finish_input_pass) (cinfo); |
| 307 | return JPEG_SCAN_COMPLETED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
| 311 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 312 | * Decompress and return some data in the multi-pass case. |
| 313 | * Always attempts to emit one fully interleaved MCU row ("iMCU" row). |
| 314 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 315 | * |
| 316 | * NB: output_buf contains a plane for each component in image. |
| 317 | */ |
| 318 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 319 | METHODDEF(int) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 320 | decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 321 | { |
| 322 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 323 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 324 | JDIMENSION block_num; |
| 325 | int ci, block_row, block_rows; |
| 326 | JBLOCKARRAY buffer; |
| 327 | JBLOCKROW buffer_ptr; |
| 328 | JSAMPARRAY output_ptr; |
| 329 | JDIMENSION output_col; |
| 330 | jpeg_component_info *compptr; |
| 331 | inverse_DCT_method_ptr inverse_DCT; |
| 332 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 333 | /* Force some input to be done if we are getting ahead of the input. */ |
| 334 | while (cinfo->input_scan_number < cinfo->output_scan_number || |
| 335 | (cinfo->input_scan_number == cinfo->output_scan_number && |
| 336 | cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { |
| 337 | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) |
| 338 | return JPEG_SUSPENDED; |
| 339 | } |
| 340 | |
| 341 | /* OK, output from the virtual arrays. */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 342 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 343 | ci++, compptr++) { |
| 344 | /* Don't bother to IDCT an uninteresting component. */ |
| 345 | if (! compptr->component_needed) |
| 346 | continue; |
| 347 | /* Align the virtual buffer for this component. */ |
| 348 | buffer = (*cinfo->mem->access_virt_barray) |
| 349 | ((j_common_ptr) cinfo, coef->whole_image[ci], |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 350 | cinfo->output_iMCU_row * compptr->v_samp_factor, |
| 351 | (JDIMENSION) compptr->v_samp_factor, FALSE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 352 | /* Count non-dummy DCT block rows in this iMCU row. */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 353 | if (cinfo->output_iMCU_row < last_iMCU_row) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 354 | block_rows = compptr->v_samp_factor; |
| 355 | else { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 356 | /* NB: can't use last_row_height here; it is input-side-dependent! */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 357 | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
| 358 | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
| 359 | } |
| 360 | inverse_DCT = cinfo->idct->inverse_DCT[ci]; |
| 361 | output_ptr = output_buf[ci]; |
| 362 | /* Loop over all DCT blocks to be processed. */ |
| 363 | for (block_row = 0; block_row < block_rows; block_row++) { |
| 364 | buffer_ptr = buffer[block_row]; |
| 365 | output_col = 0; |
| 366 | for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { |
| 367 | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, |
| 368 | output_ptr, output_col); |
| 369 | buffer_ptr++; |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 370 | output_col += compptr->_DCT_scaled_size; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 371 | } |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 372 | output_ptr += compptr->_DCT_scaled_size; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 376 | if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) |
| 377 | return JPEG_ROW_COMPLETED; |
| 378 | return JPEG_SCAN_COMPLETED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
| 382 | |
| 383 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 384 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
| 385 | |
| 386 | /* |
| 387 | * This code applies interblock smoothing as described by section K.8 |
| 388 | * of the JPEG standard: the first 5 AC coefficients are estimated from |
| 389 | * the DC values of a DCT block and its 8 neighboring blocks. |
| 390 | * We apply smoothing only for progressive JPEG decoding, and only if |
| 391 | * the coefficients it can estimate are not yet known to full precision. |
| 392 | */ |
| 393 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 394 | /* Natural-order array positions of the first 5 zigzag-order coefficients */ |
| 395 | #define Q01_POS 1 |
| 396 | #define Q10_POS 8 |
| 397 | #define Q20_POS 16 |
| 398 | #define Q11_POS 9 |
| 399 | #define Q02_POS 2 |
| 400 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 401 | /* |
| 402 | * Determine whether block smoothing is applicable and safe. |
| 403 | * We also latch the current states of the coef_bits[] entries for the |
| 404 | * AC coefficients; otherwise, if the input side of the decompressor |
| 405 | * advances into a new scan, we might think the coefficients are known |
| 406 | * more accurately than they really are. |
| 407 | */ |
| 408 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 409 | LOCAL(boolean) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 410 | smoothing_ok (j_decompress_ptr cinfo) |
| 411 | { |
| 412 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 413 | boolean smoothing_useful = FALSE; |
| 414 | int ci, coefi; |
| 415 | jpeg_component_info *compptr; |
| 416 | JQUANT_TBL * qtable; |
| 417 | int * coef_bits; |
| 418 | int * coef_bits_latch; |
| 419 | |
| 420 | if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) |
| 421 | return FALSE; |
| 422 | |
| 423 | /* Allocate latch area if not already done */ |
| 424 | if (coef->coef_bits_latch == NULL) |
| 425 | coef->coef_bits_latch = (int *) |
| 426 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 427 | cinfo->num_components * |
| 428 | (SAVED_COEFS * SIZEOF(int))); |
| 429 | coef_bits_latch = coef->coef_bits_latch; |
| 430 | |
| 431 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 432 | ci++, compptr++) { |
| 433 | /* All components' quantization values must already be latched. */ |
| 434 | if ((qtable = compptr->quant_table) == NULL) |
| 435 | return FALSE; |
| 436 | /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 437 | if (qtable->quantval[0] == 0 || |
| 438 | qtable->quantval[Q01_POS] == 0 || |
| 439 | qtable->quantval[Q10_POS] == 0 || |
| 440 | qtable->quantval[Q20_POS] == 0 || |
| 441 | qtable->quantval[Q11_POS] == 0 || |
| 442 | qtable->quantval[Q02_POS] == 0) |
| 443 | return FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 444 | /* DC values must be at least partly known for all components. */ |
| 445 | coef_bits = cinfo->coef_bits[ci]; |
| 446 | if (coef_bits[0] < 0) |
| 447 | return FALSE; |
| 448 | /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ |
| 449 | for (coefi = 1; coefi <= 5; coefi++) { |
| 450 | coef_bits_latch[coefi] = coef_bits[coefi]; |
| 451 | if (coef_bits[coefi] != 0) |
| 452 | smoothing_useful = TRUE; |
| 453 | } |
| 454 | coef_bits_latch += SAVED_COEFS; |
| 455 | } |
| 456 | |
| 457 | return smoothing_useful; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /* |
| 462 | * Variant of decompress_data for use when doing block smoothing. |
| 463 | */ |
| 464 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 465 | METHODDEF(int) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 466 | decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
| 467 | { |
| 468 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 469 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
| 470 | JDIMENSION block_num, last_block_column; |
| 471 | int ci, block_row, block_rows, access_rows; |
| 472 | JBLOCKARRAY buffer; |
| 473 | JBLOCKROW buffer_ptr, prev_block_row, next_block_row; |
| 474 | JSAMPARRAY output_ptr; |
| 475 | JDIMENSION output_col; |
| 476 | jpeg_component_info *compptr; |
| 477 | inverse_DCT_method_ptr inverse_DCT; |
| 478 | boolean first_row, last_row; |
Pierre Ossman | 35c4719 | 2009-03-09 13:29:37 +0000 | [diff] [blame] | 479 | JCOEF * workspace; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 480 | int *coef_bits; |
| 481 | JQUANT_TBL *quanttbl; |
| 482 | INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; |
| 483 | int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; |
| 484 | int Al, pred; |
| 485 | |
Pierre Ossman | 35c4719 | 2009-03-09 13:29:37 +0000 | [diff] [blame] | 486 | /* Keep a local variable to avoid looking it up more than once */ |
| 487 | workspace = coef->workspace; |
| 488 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 489 | /* Force some input to be done if we are getting ahead of the input. */ |
| 490 | while (cinfo->input_scan_number <= cinfo->output_scan_number && |
| 491 | ! cinfo->inputctl->eoi_reached) { |
| 492 | if (cinfo->input_scan_number == cinfo->output_scan_number) { |
| 493 | /* If input is working on current scan, we ordinarily want it to |
| 494 | * have completed the current row. But if input scan is DC, |
| 495 | * we want it to keep one row ahead so that next block row's DC |
| 496 | * values are up to date. |
| 497 | */ |
| 498 | JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; |
| 499 | if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) |
| 500 | break; |
| 501 | } |
| 502 | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) |
| 503 | return JPEG_SUSPENDED; |
| 504 | } |
| 505 | |
| 506 | /* OK, output from the virtual arrays. */ |
| 507 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 508 | ci++, compptr++) { |
| 509 | /* Don't bother to IDCT an uninteresting component. */ |
| 510 | if (! compptr->component_needed) |
| 511 | continue; |
| 512 | /* Count non-dummy DCT block rows in this iMCU row. */ |
| 513 | if (cinfo->output_iMCU_row < last_iMCU_row) { |
| 514 | block_rows = compptr->v_samp_factor; |
| 515 | access_rows = block_rows * 2; /* this and next iMCU row */ |
| 516 | last_row = FALSE; |
| 517 | } else { |
| 518 | /* NB: can't use last_row_height here; it is input-side-dependent! */ |
| 519 | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
| 520 | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
| 521 | access_rows = block_rows; /* this iMCU row only */ |
| 522 | last_row = TRUE; |
| 523 | } |
| 524 | /* Align the virtual buffer for this component. */ |
| 525 | if (cinfo->output_iMCU_row > 0) { |
| 526 | access_rows += compptr->v_samp_factor; /* prior iMCU row too */ |
| 527 | buffer = (*cinfo->mem->access_virt_barray) |
| 528 | ((j_common_ptr) cinfo, coef->whole_image[ci], |
| 529 | (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, |
| 530 | (JDIMENSION) access_rows, FALSE); |
| 531 | buffer += compptr->v_samp_factor; /* point to current iMCU row */ |
| 532 | first_row = FALSE; |
| 533 | } else { |
| 534 | buffer = (*cinfo->mem->access_virt_barray) |
| 535 | ((j_common_ptr) cinfo, coef->whole_image[ci], |
| 536 | (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); |
| 537 | first_row = TRUE; |
| 538 | } |
| 539 | /* Fetch component-dependent info */ |
| 540 | coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); |
| 541 | quanttbl = compptr->quant_table; |
| 542 | Q00 = quanttbl->quantval[0]; |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 543 | Q01 = quanttbl->quantval[Q01_POS]; |
| 544 | Q10 = quanttbl->quantval[Q10_POS]; |
| 545 | Q20 = quanttbl->quantval[Q20_POS]; |
| 546 | Q11 = quanttbl->quantval[Q11_POS]; |
| 547 | Q02 = quanttbl->quantval[Q02_POS]; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 548 | inverse_DCT = cinfo->idct->inverse_DCT[ci]; |
| 549 | output_ptr = output_buf[ci]; |
| 550 | /* Loop over all DCT blocks to be processed. */ |
| 551 | for (block_row = 0; block_row < block_rows; block_row++) { |
| 552 | buffer_ptr = buffer[block_row]; |
| 553 | if (first_row && block_row == 0) |
| 554 | prev_block_row = buffer_ptr; |
| 555 | else |
| 556 | prev_block_row = buffer[block_row-1]; |
| 557 | if (last_row && block_row == block_rows-1) |
| 558 | next_block_row = buffer_ptr; |
| 559 | else |
| 560 | next_block_row = buffer[block_row+1]; |
| 561 | /* We fetch the surrounding DC values using a sliding-register approach. |
| 562 | * Initialize all nine here so as to do the right thing on narrow pics. |
| 563 | */ |
| 564 | DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; |
| 565 | DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; |
| 566 | DC7 = DC8 = DC9 = (int) next_block_row[0][0]; |
| 567 | output_col = 0; |
| 568 | last_block_column = compptr->width_in_blocks - 1; |
| 569 | for (block_num = 0; block_num <= last_block_column; block_num++) { |
| 570 | /* Fetch current DCT block into workspace so we can modify it. */ |
| 571 | jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); |
| 572 | /* Update DC values */ |
| 573 | if (block_num < last_block_column) { |
| 574 | DC3 = (int) prev_block_row[1][0]; |
| 575 | DC6 = (int) buffer_ptr[1][0]; |
| 576 | DC9 = (int) next_block_row[1][0]; |
| 577 | } |
| 578 | /* Compute coefficient estimates per K.8. |
| 579 | * An estimate is applied only if coefficient is still zero, |
| 580 | * and is not known to be fully accurate. |
| 581 | */ |
| 582 | /* AC01 */ |
| 583 | if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { |
| 584 | num = 36 * Q00 * (DC4 - DC6); |
| 585 | if (num >= 0) { |
| 586 | pred = (int) (((Q01<<7) + num) / (Q01<<8)); |
| 587 | if (Al > 0 && pred >= (1<<Al)) |
| 588 | pred = (1<<Al)-1; |
| 589 | } else { |
| 590 | pred = (int) (((Q01<<7) - num) / (Q01<<8)); |
| 591 | if (Al > 0 && pred >= (1<<Al)) |
| 592 | pred = (1<<Al)-1; |
| 593 | pred = -pred; |
| 594 | } |
| 595 | workspace[1] = (JCOEF) pred; |
| 596 | } |
| 597 | /* AC10 */ |
| 598 | if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) { |
| 599 | num = 36 * Q00 * (DC2 - DC8); |
| 600 | if (num >= 0) { |
| 601 | pred = (int) (((Q10<<7) + num) / (Q10<<8)); |
| 602 | if (Al > 0 && pred >= (1<<Al)) |
| 603 | pred = (1<<Al)-1; |
| 604 | } else { |
| 605 | pred = (int) (((Q10<<7) - num) / (Q10<<8)); |
| 606 | if (Al > 0 && pred >= (1<<Al)) |
| 607 | pred = (1<<Al)-1; |
| 608 | pred = -pred; |
| 609 | } |
| 610 | workspace[8] = (JCOEF) pred; |
| 611 | } |
| 612 | /* AC20 */ |
| 613 | if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) { |
| 614 | num = 9 * Q00 * (DC2 + DC8 - 2*DC5); |
| 615 | if (num >= 0) { |
| 616 | pred = (int) (((Q20<<7) + num) / (Q20<<8)); |
| 617 | if (Al > 0 && pred >= (1<<Al)) |
| 618 | pred = (1<<Al)-1; |
| 619 | } else { |
| 620 | pred = (int) (((Q20<<7) - num) / (Q20<<8)); |
| 621 | if (Al > 0 && pred >= (1<<Al)) |
| 622 | pred = (1<<Al)-1; |
| 623 | pred = -pred; |
| 624 | } |
| 625 | workspace[16] = (JCOEF) pred; |
| 626 | } |
| 627 | /* AC11 */ |
| 628 | if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) { |
| 629 | num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9); |
| 630 | if (num >= 0) { |
| 631 | pred = (int) (((Q11<<7) + num) / (Q11<<8)); |
| 632 | if (Al > 0 && pred >= (1<<Al)) |
| 633 | pred = (1<<Al)-1; |
| 634 | } else { |
| 635 | pred = (int) (((Q11<<7) - num) / (Q11<<8)); |
| 636 | if (Al > 0 && pred >= (1<<Al)) |
| 637 | pred = (1<<Al)-1; |
| 638 | pred = -pred; |
| 639 | } |
| 640 | workspace[9] = (JCOEF) pred; |
| 641 | } |
| 642 | /* AC02 */ |
| 643 | if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) { |
| 644 | num = 9 * Q00 * (DC4 + DC6 - 2*DC5); |
| 645 | if (num >= 0) { |
| 646 | pred = (int) (((Q02<<7) + num) / (Q02<<8)); |
| 647 | if (Al > 0 && pred >= (1<<Al)) |
| 648 | pred = (1<<Al)-1; |
| 649 | } else { |
| 650 | pred = (int) (((Q02<<7) - num) / (Q02<<8)); |
| 651 | if (Al > 0 && pred >= (1<<Al)) |
| 652 | pred = (1<<Al)-1; |
| 653 | pred = -pred; |
| 654 | } |
| 655 | workspace[2] = (JCOEF) pred; |
| 656 | } |
| 657 | /* OK, do the IDCT */ |
| 658 | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace, |
| 659 | output_ptr, output_col); |
| 660 | /* Advance for next column */ |
| 661 | DC1 = DC2; DC2 = DC3; |
| 662 | DC4 = DC5; DC5 = DC6; |
| 663 | DC7 = DC8; DC8 = DC9; |
| 664 | buffer_ptr++, prev_block_row++, next_block_row++; |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 665 | output_col += compptr->_DCT_scaled_size; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 666 | } |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 667 | output_ptr += compptr->_DCT_scaled_size; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) |
| 672 | return JPEG_ROW_COMPLETED; |
| 673 | return JPEG_SCAN_COMPLETED; |
| 674 | } |
| 675 | |
| 676 | #endif /* BLOCK_SMOOTHING_SUPPORTED */ |
| 677 | |
| 678 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 679 | /* |
| 680 | * Initialize coefficient buffer controller. |
| 681 | */ |
| 682 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 683 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 684 | jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) |
| 685 | { |
| 686 | my_coef_ptr coef; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 687 | |
| 688 | coef = (my_coef_ptr) |
| 689 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 690 | SIZEOF(my_coef_controller)); |
| 691 | cinfo->coef = (struct jpeg_d_coef_controller *) coef; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 692 | coef->pub.start_input_pass = start_input_pass; |
| 693 | coef->pub.start_output_pass = start_output_pass; |
| 694 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
| 695 | coef->coef_bits_latch = NULL; |
| 696 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 697 | |
| 698 | /* Create the coefficient buffer. */ |
| 699 | if (need_full_buffer) { |
| 700 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 701 | /* Allocate a full-image virtual array for each component, */ |
| 702 | /* padded to a multiple of samp_factor DCT blocks in each direction. */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 703 | /* Note we ask for a pre-zeroed array. */ |
| 704 | int ci, access_rows; |
| 705 | jpeg_component_info *compptr; |
| 706 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 707 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 708 | ci++, compptr++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 709 | access_rows = compptr->v_samp_factor; |
| 710 | #ifdef BLOCK_SMOOTHING_SUPPORTED |
| 711 | /* If block smoothing could be used, need a bigger window */ |
| 712 | if (cinfo->progressive_mode) |
| 713 | access_rows *= 3; |
| 714 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 715 | coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 716 | ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 717 | (JDIMENSION) jround_up((long) compptr->width_in_blocks, |
| 718 | (long) compptr->h_samp_factor), |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 719 | (JDIMENSION) jround_up((long) compptr->height_in_blocks, |
| 720 | (long) compptr->v_samp_factor), |
| 721 | (JDIMENSION) access_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 722 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 723 | coef->pub.consume_data = consume_data; |
| 724 | coef->pub.decompress_data = decompress_data; |
| 725 | coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 726 | #else |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 727 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 728 | #endif |
| 729 | } else { |
| 730 | /* We only need a single-MCU buffer. */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 731 | JBLOCKROW buffer; |
| 732 | int i; |
| 733 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 734 | buffer = (JBLOCKROW) |
| 735 | (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 736 | D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); |
| 737 | for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 738 | coef->MCU_buffer[i] = buffer + i; |
| 739 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 740 | coef->pub.consume_data = dummy_consume_data; |
| 741 | coef->pub.decompress_data = decompress_onepass; |
| 742 | coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 743 | } |
Pierre Ossman | 35c4719 | 2009-03-09 13:29:37 +0000 | [diff] [blame] | 744 | |
| 745 | /* Allocate the workspace buffer */ |
| 746 | coef->workspace = (JCOEF *) |
| 747 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 748 | SIZEOF(JCOEF) * DCTSIZE2); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 749 | } |