3dphotoshoot

annotate libs/libjpeg/jdcoefct.c @ 17:aef7f51f6397

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