dbf-halloween2015

annotate libs/libjpeg/jdcoefct.c @ 3:c37fe5d8a4ed

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