3dphotoshoot
diff libs/libjpeg/jcphuff.c @ 14:06dc8b9b4f89
added libimago, libjpeg and libpng
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 07 Jun 2015 17:25:49 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jcphuff.c Sun Jun 07 17:25:49 2015 +0300 1.3 @@ -0,0 +1,833 @@ 1.4 +/* 1.5 + * jcphuff.c 1.6 + * 1.7 + * Copyright (C) 1995-1997, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains Huffman entropy encoding routines for progressive JPEG. 1.12 + * 1.13 + * We do not support output suspension in this module, since the library 1.14 + * currently does not allow multiple-scan files to be written with output 1.15 + * suspension. 1.16 + */ 1.17 + 1.18 +#define JPEG_INTERNALS 1.19 +#include "jinclude.h" 1.20 +#include "jpeglib.h" 1.21 +#include "jchuff.h" /* Declarations shared with jchuff.c */ 1.22 + 1.23 +#ifdef C_PROGRESSIVE_SUPPORTED 1.24 + 1.25 +/* Expanded entropy encoder object for progressive Huffman encoding. */ 1.26 + 1.27 +typedef struct { 1.28 + struct jpeg_entropy_encoder pub; /* public fields */ 1.29 + 1.30 + /* Mode flag: TRUE for optimization, FALSE for actual data output */ 1.31 + boolean gather_statistics; 1.32 + 1.33 + /* Bit-level coding status. 1.34 + * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. 1.35 + */ 1.36 + JOCTET * next_output_byte; /* => next byte to write in buffer */ 1.37 + size_t free_in_buffer; /* # of byte spaces remaining in buffer */ 1.38 + INT32 put_buffer; /* current bit-accumulation buffer */ 1.39 + int put_bits; /* # of bits now in it */ 1.40 + j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ 1.41 + 1.42 + /* Coding status for DC components */ 1.43 + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 1.44 + 1.45 + /* Coding status for AC components */ 1.46 + int ac_tbl_no; /* the table number of the single component */ 1.47 + unsigned int EOBRUN; /* run length of EOBs */ 1.48 + unsigned int BE; /* # of buffered correction bits before MCU */ 1.49 + char * bit_buffer; /* buffer for correction bits (1 per char) */ 1.50 + /* packing correction bits tightly would save some space but cost time... */ 1.51 + 1.52 + unsigned int restarts_to_go; /* MCUs left in this restart interval */ 1.53 + int next_restart_num; /* next restart number to write (0-7) */ 1.54 + 1.55 + /* Pointers to derived tables (these workspaces have image lifespan). 1.56 + * Since any one scan codes only DC or only AC, we only need one set 1.57 + * of tables, not one for DC and one for AC. 1.58 + */ 1.59 + c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; 1.60 + 1.61 + /* Statistics tables for optimization; again, one set is enough */ 1.62 + long * count_ptrs[NUM_HUFF_TBLS]; 1.63 +} phuff_entropy_encoder; 1.64 + 1.65 +typedef phuff_entropy_encoder * phuff_entropy_ptr; 1.66 + 1.67 +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit 1.68 + * buffer can hold. Larger sizes may slightly improve compression, but 1.69 + * 1000 is already well into the realm of overkill. 1.70 + * The minimum safe size is 64 bits. 1.71 + */ 1.72 + 1.73 +#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ 1.74 + 1.75 +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. 1.76 + * We assume that int right shift is unsigned if INT32 right shift is, 1.77 + * which should be safe. 1.78 + */ 1.79 + 1.80 +#ifdef RIGHT_SHIFT_IS_UNSIGNED 1.81 +#define ISHIFT_TEMPS int ishift_temp; 1.82 +#define IRIGHT_SHIFT(x,shft) \ 1.83 + ((ishift_temp = (x)) < 0 ? \ 1.84 + (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ 1.85 + (ishift_temp >> (shft))) 1.86 +#else 1.87 +#define ISHIFT_TEMPS 1.88 +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) 1.89 +#endif 1.90 + 1.91 +/* Forward declarations */ 1.92 +METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, 1.93 + JBLOCKROW *MCU_data)); 1.94 +METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, 1.95 + JBLOCKROW *MCU_data)); 1.96 +METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, 1.97 + JBLOCKROW *MCU_data)); 1.98 +METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, 1.99 + JBLOCKROW *MCU_data)); 1.100 +METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); 1.101 +METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); 1.102 + 1.103 + 1.104 +/* 1.105 + * Initialize for a Huffman-compressed scan using progressive JPEG. 1.106 + */ 1.107 + 1.108 +METHODDEF(void) 1.109 +start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) 1.110 +{ 1.111 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.112 + boolean is_DC_band; 1.113 + int ci, tbl; 1.114 + jpeg_component_info * compptr; 1.115 + 1.116 + entropy->cinfo = cinfo; 1.117 + entropy->gather_statistics = gather_statistics; 1.118 + 1.119 + is_DC_band = (cinfo->Ss == 0); 1.120 + 1.121 + /* We assume jcmaster.c already validated the scan parameters. */ 1.122 + 1.123 + /* Select execution routines */ 1.124 + if (cinfo->Ah == 0) { 1.125 + if (is_DC_band) 1.126 + entropy->pub.encode_mcu = encode_mcu_DC_first; 1.127 + else 1.128 + entropy->pub.encode_mcu = encode_mcu_AC_first; 1.129 + } else { 1.130 + if (is_DC_band) 1.131 + entropy->pub.encode_mcu = encode_mcu_DC_refine; 1.132 + else { 1.133 + entropy->pub.encode_mcu = encode_mcu_AC_refine; 1.134 + /* AC refinement needs a correction bit buffer */ 1.135 + if (entropy->bit_buffer == NULL) 1.136 + entropy->bit_buffer = (char *) 1.137 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.138 + MAX_CORR_BITS * SIZEOF(char)); 1.139 + } 1.140 + } 1.141 + if (gather_statistics) 1.142 + entropy->pub.finish_pass = finish_pass_gather_phuff; 1.143 + else 1.144 + entropy->pub.finish_pass = finish_pass_phuff; 1.145 + 1.146 + /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 1.147 + * for AC coefficients. 1.148 + */ 1.149 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.150 + compptr = cinfo->cur_comp_info[ci]; 1.151 + /* Initialize DC predictions to 0 */ 1.152 + entropy->last_dc_val[ci] = 0; 1.153 + /* Get table index */ 1.154 + if (is_DC_band) { 1.155 + if (cinfo->Ah != 0) /* DC refinement needs no table */ 1.156 + continue; 1.157 + tbl = compptr->dc_tbl_no; 1.158 + } else { 1.159 + entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; 1.160 + } 1.161 + if (gather_statistics) { 1.162 + /* Check for invalid table index */ 1.163 + /* (make_c_derived_tbl does this in the other path) */ 1.164 + if (tbl < 0 || tbl >= NUM_HUFF_TBLS) 1.165 + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); 1.166 + /* Allocate and zero the statistics tables */ 1.167 + /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ 1.168 + if (entropy->count_ptrs[tbl] == NULL) 1.169 + entropy->count_ptrs[tbl] = (long *) 1.170 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.171 + 257 * SIZEOF(long)); 1.172 + MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); 1.173 + } else { 1.174 + /* Compute derived values for Huffman table */ 1.175 + /* We may do this more than once for a table, but it's not expensive */ 1.176 + jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, 1.177 + & entropy->derived_tbls[tbl]); 1.178 + } 1.179 + } 1.180 + 1.181 + /* Initialize AC stuff */ 1.182 + entropy->EOBRUN = 0; 1.183 + entropy->BE = 0; 1.184 + 1.185 + /* Initialize bit buffer to empty */ 1.186 + entropy->put_buffer = 0; 1.187 + entropy->put_bits = 0; 1.188 + 1.189 + /* Initialize restart stuff */ 1.190 + entropy->restarts_to_go = cinfo->restart_interval; 1.191 + entropy->next_restart_num = 0; 1.192 +} 1.193 + 1.194 + 1.195 +/* Outputting bytes to the file. 1.196 + * NB: these must be called only when actually outputting, 1.197 + * that is, entropy->gather_statistics == FALSE. 1.198 + */ 1.199 + 1.200 +/* Emit a byte */ 1.201 +#define emit_byte(entropy,val) \ 1.202 + { *(entropy)->next_output_byte++ = (JOCTET) (val); \ 1.203 + if (--(entropy)->free_in_buffer == 0) \ 1.204 + dump_buffer(entropy); } 1.205 + 1.206 + 1.207 +LOCAL(void) 1.208 +dump_buffer (phuff_entropy_ptr entropy) 1.209 +/* Empty the output buffer; we do not support suspension in this module. */ 1.210 +{ 1.211 + struct jpeg_destination_mgr * dest = entropy->cinfo->dest; 1.212 + 1.213 + if (! (*dest->empty_output_buffer) (entropy->cinfo)) 1.214 + ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); 1.215 + /* After a successful buffer dump, must reset buffer pointers */ 1.216 + entropy->next_output_byte = dest->next_output_byte; 1.217 + entropy->free_in_buffer = dest->free_in_buffer; 1.218 +} 1.219 + 1.220 + 1.221 +/* Outputting bits to the file */ 1.222 + 1.223 +/* Only the right 24 bits of put_buffer are used; the valid bits are 1.224 + * left-justified in this part. At most 16 bits can be passed to emit_bits 1.225 + * in one call, and we never retain more than 7 bits in put_buffer 1.226 + * between calls, so 24 bits are sufficient. 1.227 + */ 1.228 + 1.229 +INLINE 1.230 +LOCAL(void) 1.231 +emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) 1.232 +/* Emit some bits, unless we are in gather mode */ 1.233 +{ 1.234 + /* This routine is heavily used, so it's worth coding tightly. */ 1.235 + register INT32 put_buffer = (INT32) code; 1.236 + register int put_bits = entropy->put_bits; 1.237 + 1.238 + /* if size is 0, caller used an invalid Huffman table entry */ 1.239 + if (size == 0) 1.240 + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); 1.241 + 1.242 + if (entropy->gather_statistics) 1.243 + return; /* do nothing if we're only getting stats */ 1.244 + 1.245 + put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ 1.246 + 1.247 + put_bits += size; /* new number of bits in buffer */ 1.248 + 1.249 + put_buffer <<= 24 - put_bits; /* align incoming bits */ 1.250 + 1.251 + put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ 1.252 + 1.253 + while (put_bits >= 8) { 1.254 + int c = (int) ((put_buffer >> 16) & 0xFF); 1.255 + 1.256 + emit_byte(entropy, c); 1.257 + if (c == 0xFF) { /* need to stuff a zero byte? */ 1.258 + emit_byte(entropy, 0); 1.259 + } 1.260 + put_buffer <<= 8; 1.261 + put_bits -= 8; 1.262 + } 1.263 + 1.264 + entropy->put_buffer = put_buffer; /* update variables */ 1.265 + entropy->put_bits = put_bits; 1.266 +} 1.267 + 1.268 + 1.269 +LOCAL(void) 1.270 +flush_bits (phuff_entropy_ptr entropy) 1.271 +{ 1.272 + emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ 1.273 + entropy->put_buffer = 0; /* and reset bit-buffer to empty */ 1.274 + entropy->put_bits = 0; 1.275 +} 1.276 + 1.277 + 1.278 +/* 1.279 + * Emit (or just count) a Huffman symbol. 1.280 + */ 1.281 + 1.282 +INLINE 1.283 +LOCAL(void) 1.284 +emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) 1.285 +{ 1.286 + if (entropy->gather_statistics) 1.287 + entropy->count_ptrs[tbl_no][symbol]++; 1.288 + else { 1.289 + c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; 1.290 + emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); 1.291 + } 1.292 +} 1.293 + 1.294 + 1.295 +/* 1.296 + * Emit bits from a correction bit buffer. 1.297 + */ 1.298 + 1.299 +LOCAL(void) 1.300 +emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, 1.301 + unsigned int nbits) 1.302 +{ 1.303 + if (entropy->gather_statistics) 1.304 + return; /* no real work */ 1.305 + 1.306 + while (nbits > 0) { 1.307 + emit_bits(entropy, (unsigned int) (*bufstart), 1); 1.308 + bufstart++; 1.309 + nbits--; 1.310 + } 1.311 +} 1.312 + 1.313 + 1.314 +/* 1.315 + * Emit any pending EOBRUN symbol. 1.316 + */ 1.317 + 1.318 +LOCAL(void) 1.319 +emit_eobrun (phuff_entropy_ptr entropy) 1.320 +{ 1.321 + register int temp, nbits; 1.322 + 1.323 + if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ 1.324 + temp = entropy->EOBRUN; 1.325 + nbits = 0; 1.326 + while ((temp >>= 1)) 1.327 + nbits++; 1.328 + /* safety check: shouldn't happen given limited correction-bit buffer */ 1.329 + if (nbits > 14) 1.330 + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); 1.331 + 1.332 + emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); 1.333 + if (nbits) 1.334 + emit_bits(entropy, entropy->EOBRUN, nbits); 1.335 + 1.336 + entropy->EOBRUN = 0; 1.337 + 1.338 + /* Emit any buffered correction bits */ 1.339 + emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); 1.340 + entropy->BE = 0; 1.341 + } 1.342 +} 1.343 + 1.344 + 1.345 +/* 1.346 + * Emit a restart marker & resynchronize predictions. 1.347 + */ 1.348 + 1.349 +LOCAL(void) 1.350 +emit_restart (phuff_entropy_ptr entropy, int restart_num) 1.351 +{ 1.352 + int ci; 1.353 + 1.354 + emit_eobrun(entropy); 1.355 + 1.356 + if (! entropy->gather_statistics) { 1.357 + flush_bits(entropy); 1.358 + emit_byte(entropy, 0xFF); 1.359 + emit_byte(entropy, JPEG_RST0 + restart_num); 1.360 + } 1.361 + 1.362 + if (entropy->cinfo->Ss == 0) { 1.363 + /* Re-initialize DC predictions to 0 */ 1.364 + for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) 1.365 + entropy->last_dc_val[ci] = 0; 1.366 + } else { 1.367 + /* Re-initialize all AC-related fields to 0 */ 1.368 + entropy->EOBRUN = 0; 1.369 + entropy->BE = 0; 1.370 + } 1.371 +} 1.372 + 1.373 + 1.374 +/* 1.375 + * MCU encoding for DC initial scan (either spectral selection, 1.376 + * or first pass of successive approximation). 1.377 + */ 1.378 + 1.379 +METHODDEF(boolean) 1.380 +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.381 +{ 1.382 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.383 + register int temp, temp2; 1.384 + register int nbits; 1.385 + int blkn, ci; 1.386 + int Al = cinfo->Al; 1.387 + JBLOCKROW block; 1.388 + jpeg_component_info * compptr; 1.389 + ISHIFT_TEMPS 1.390 + 1.391 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.392 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.393 + 1.394 + /* Emit restart marker if needed */ 1.395 + if (cinfo->restart_interval) 1.396 + if (entropy->restarts_to_go == 0) 1.397 + emit_restart(entropy, entropy->next_restart_num); 1.398 + 1.399 + /* Encode the MCU data blocks */ 1.400 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.401 + block = MCU_data[blkn]; 1.402 + ci = cinfo->MCU_membership[blkn]; 1.403 + compptr = cinfo->cur_comp_info[ci]; 1.404 + 1.405 + /* Compute the DC value after the required point transform by Al. 1.406 + * This is simply an arithmetic right shift. 1.407 + */ 1.408 + temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); 1.409 + 1.410 + /* DC differences are figured on the point-transformed values. */ 1.411 + temp = temp2 - entropy->last_dc_val[ci]; 1.412 + entropy->last_dc_val[ci] = temp2; 1.413 + 1.414 + /* Encode the DC coefficient difference per section G.1.2.1 */ 1.415 + temp2 = temp; 1.416 + if (temp < 0) { 1.417 + temp = -temp; /* temp is abs value of input */ 1.418 + /* For a negative input, want temp2 = bitwise complement of abs(input) */ 1.419 + /* This code assumes we are on a two's complement machine */ 1.420 + temp2--; 1.421 + } 1.422 + 1.423 + /* Find the number of bits needed for the magnitude of the coefficient */ 1.424 + nbits = 0; 1.425 + while (temp) { 1.426 + nbits++; 1.427 + temp >>= 1; 1.428 + } 1.429 + /* Check for out-of-range coefficient values. 1.430 + * Since we're encoding a difference, the range limit is twice as much. 1.431 + */ 1.432 + if (nbits > MAX_COEF_BITS+1) 1.433 + ERREXIT(cinfo, JERR_BAD_DCT_COEF); 1.434 + 1.435 + /* Count/emit the Huffman-coded symbol for the number of bits */ 1.436 + emit_symbol(entropy, compptr->dc_tbl_no, nbits); 1.437 + 1.438 + /* Emit that number of bits of the value, if positive, */ 1.439 + /* or the complement of its magnitude, if negative. */ 1.440 + if (nbits) /* emit_bits rejects calls with size 0 */ 1.441 + emit_bits(entropy, (unsigned int) temp2, nbits); 1.442 + } 1.443 + 1.444 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.445 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.446 + 1.447 + /* Update restart-interval state too */ 1.448 + if (cinfo->restart_interval) { 1.449 + if (entropy->restarts_to_go == 0) { 1.450 + entropy->restarts_to_go = cinfo->restart_interval; 1.451 + entropy->next_restart_num++; 1.452 + entropy->next_restart_num &= 7; 1.453 + } 1.454 + entropy->restarts_to_go--; 1.455 + } 1.456 + 1.457 + return TRUE; 1.458 +} 1.459 + 1.460 + 1.461 +/* 1.462 + * MCU encoding for AC initial scan (either spectral selection, 1.463 + * or first pass of successive approximation). 1.464 + */ 1.465 + 1.466 +METHODDEF(boolean) 1.467 +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.468 +{ 1.469 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.470 + register int temp, temp2; 1.471 + register int nbits; 1.472 + register int r, k; 1.473 + int Se = cinfo->Se; 1.474 + int Al = cinfo->Al; 1.475 + JBLOCKROW block; 1.476 + 1.477 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.478 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.479 + 1.480 + /* Emit restart marker if needed */ 1.481 + if (cinfo->restart_interval) 1.482 + if (entropy->restarts_to_go == 0) 1.483 + emit_restart(entropy, entropy->next_restart_num); 1.484 + 1.485 + /* Encode the MCU data block */ 1.486 + block = MCU_data[0]; 1.487 + 1.488 + /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ 1.489 + 1.490 + r = 0; /* r = run length of zeros */ 1.491 + 1.492 + for (k = cinfo->Ss; k <= Se; k++) { 1.493 + if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { 1.494 + r++; 1.495 + continue; 1.496 + } 1.497 + /* We must apply the point transform by Al. For AC coefficients this 1.498 + * is an integer division with rounding towards 0. To do this portably 1.499 + * in C, we shift after obtaining the absolute value; so the code is 1.500 + * interwoven with finding the abs value (temp) and output bits (temp2). 1.501 + */ 1.502 + if (temp < 0) { 1.503 + temp = -temp; /* temp is abs value of input */ 1.504 + temp >>= Al; /* apply the point transform */ 1.505 + /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ 1.506 + temp2 = ~temp; 1.507 + } else { 1.508 + temp >>= Al; /* apply the point transform */ 1.509 + temp2 = temp; 1.510 + } 1.511 + /* Watch out for case that nonzero coef is zero after point transform */ 1.512 + if (temp == 0) { 1.513 + r++; 1.514 + continue; 1.515 + } 1.516 + 1.517 + /* Emit any pending EOBRUN */ 1.518 + if (entropy->EOBRUN > 0) 1.519 + emit_eobrun(entropy); 1.520 + /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 1.521 + while (r > 15) { 1.522 + emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); 1.523 + r -= 16; 1.524 + } 1.525 + 1.526 + /* Find the number of bits needed for the magnitude of the coefficient */ 1.527 + nbits = 1; /* there must be at least one 1 bit */ 1.528 + while ((temp >>= 1)) 1.529 + nbits++; 1.530 + /* Check for out-of-range coefficient values */ 1.531 + if (nbits > MAX_COEF_BITS) 1.532 + ERREXIT(cinfo, JERR_BAD_DCT_COEF); 1.533 + 1.534 + /* Count/emit Huffman symbol for run length / number of bits */ 1.535 + emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); 1.536 + 1.537 + /* Emit that number of bits of the value, if positive, */ 1.538 + /* or the complement of its magnitude, if negative. */ 1.539 + emit_bits(entropy, (unsigned int) temp2, nbits); 1.540 + 1.541 + r = 0; /* reset zero run length */ 1.542 + } 1.543 + 1.544 + if (r > 0) { /* If there are trailing zeroes, */ 1.545 + entropy->EOBRUN++; /* count an EOB */ 1.546 + if (entropy->EOBRUN == 0x7FFF) 1.547 + emit_eobrun(entropy); /* force it out to avoid overflow */ 1.548 + } 1.549 + 1.550 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.551 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.552 + 1.553 + /* Update restart-interval state too */ 1.554 + if (cinfo->restart_interval) { 1.555 + if (entropy->restarts_to_go == 0) { 1.556 + entropy->restarts_to_go = cinfo->restart_interval; 1.557 + entropy->next_restart_num++; 1.558 + entropy->next_restart_num &= 7; 1.559 + } 1.560 + entropy->restarts_to_go--; 1.561 + } 1.562 + 1.563 + return TRUE; 1.564 +} 1.565 + 1.566 + 1.567 +/* 1.568 + * MCU encoding for DC successive approximation refinement scan. 1.569 + * Note: we assume such scans can be multi-component, although the spec 1.570 + * is not very clear on the point. 1.571 + */ 1.572 + 1.573 +METHODDEF(boolean) 1.574 +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.575 +{ 1.576 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.577 + register int temp; 1.578 + int blkn; 1.579 + int Al = cinfo->Al; 1.580 + JBLOCKROW block; 1.581 + 1.582 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.583 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.584 + 1.585 + /* Emit restart marker if needed */ 1.586 + if (cinfo->restart_interval) 1.587 + if (entropy->restarts_to_go == 0) 1.588 + emit_restart(entropy, entropy->next_restart_num); 1.589 + 1.590 + /* Encode the MCU data blocks */ 1.591 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.592 + block = MCU_data[blkn]; 1.593 + 1.594 + /* We simply emit the Al'th bit of the DC coefficient value. */ 1.595 + temp = (*block)[0]; 1.596 + emit_bits(entropy, (unsigned int) (temp >> Al), 1); 1.597 + } 1.598 + 1.599 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.600 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.601 + 1.602 + /* Update restart-interval state too */ 1.603 + if (cinfo->restart_interval) { 1.604 + if (entropy->restarts_to_go == 0) { 1.605 + entropy->restarts_to_go = cinfo->restart_interval; 1.606 + entropy->next_restart_num++; 1.607 + entropy->next_restart_num &= 7; 1.608 + } 1.609 + entropy->restarts_to_go--; 1.610 + } 1.611 + 1.612 + return TRUE; 1.613 +} 1.614 + 1.615 + 1.616 +/* 1.617 + * MCU encoding for AC successive approximation refinement scan. 1.618 + */ 1.619 + 1.620 +METHODDEF(boolean) 1.621 +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.622 +{ 1.623 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.624 + register int temp; 1.625 + register int r, k; 1.626 + int EOB; 1.627 + char *BR_buffer; 1.628 + unsigned int BR; 1.629 + int Se = cinfo->Se; 1.630 + int Al = cinfo->Al; 1.631 + JBLOCKROW block; 1.632 + int absvalues[DCTSIZE2]; 1.633 + 1.634 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.635 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.636 + 1.637 + /* Emit restart marker if needed */ 1.638 + if (cinfo->restart_interval) 1.639 + if (entropy->restarts_to_go == 0) 1.640 + emit_restart(entropy, entropy->next_restart_num); 1.641 + 1.642 + /* Encode the MCU data block */ 1.643 + block = MCU_data[0]; 1.644 + 1.645 + /* It is convenient to make a pre-pass to determine the transformed 1.646 + * coefficients' absolute values and the EOB position. 1.647 + */ 1.648 + EOB = 0; 1.649 + for (k = cinfo->Ss; k <= Se; k++) { 1.650 + temp = (*block)[jpeg_natural_order[k]]; 1.651 + /* We must apply the point transform by Al. For AC coefficients this 1.652 + * is an integer division with rounding towards 0. To do this portably 1.653 + * in C, we shift after obtaining the absolute value. 1.654 + */ 1.655 + if (temp < 0) 1.656 + temp = -temp; /* temp is abs value of input */ 1.657 + temp >>= Al; /* apply the point transform */ 1.658 + absvalues[k] = temp; /* save abs value for main pass */ 1.659 + if (temp == 1) 1.660 + EOB = k; /* EOB = index of last newly-nonzero coef */ 1.661 + } 1.662 + 1.663 + /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ 1.664 + 1.665 + r = 0; /* r = run length of zeros */ 1.666 + BR = 0; /* BR = count of buffered bits added now */ 1.667 + BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ 1.668 + 1.669 + for (k = cinfo->Ss; k <= Se; k++) { 1.670 + if ((temp = absvalues[k]) == 0) { 1.671 + r++; 1.672 + continue; 1.673 + } 1.674 + 1.675 + /* Emit any required ZRLs, but not if they can be folded into EOB */ 1.676 + while (r > 15 && k <= EOB) { 1.677 + /* emit any pending EOBRUN and the BE correction bits */ 1.678 + emit_eobrun(entropy); 1.679 + /* Emit ZRL */ 1.680 + emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); 1.681 + r -= 16; 1.682 + /* Emit buffered correction bits that must be associated with ZRL */ 1.683 + emit_buffered_bits(entropy, BR_buffer, BR); 1.684 + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ 1.685 + BR = 0; 1.686 + } 1.687 + 1.688 + /* If the coef was previously nonzero, it only needs a correction bit. 1.689 + * NOTE: a straight translation of the spec's figure G.7 would suggest 1.690 + * that we also need to test r > 15. But if r > 15, we can only get here 1.691 + * if k > EOB, which implies that this coefficient is not 1. 1.692 + */ 1.693 + if (temp > 1) { 1.694 + /* The correction bit is the next bit of the absolute value. */ 1.695 + BR_buffer[BR++] = (char) (temp & 1); 1.696 + continue; 1.697 + } 1.698 + 1.699 + /* Emit any pending EOBRUN and the BE correction bits */ 1.700 + emit_eobrun(entropy); 1.701 + 1.702 + /* Count/emit Huffman symbol for run length / number of bits */ 1.703 + emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); 1.704 + 1.705 + /* Emit output bit for newly-nonzero coef */ 1.706 + temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; 1.707 + emit_bits(entropy, (unsigned int) temp, 1); 1.708 + 1.709 + /* Emit buffered correction bits that must be associated with this code */ 1.710 + emit_buffered_bits(entropy, BR_buffer, BR); 1.711 + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ 1.712 + BR = 0; 1.713 + r = 0; /* reset zero run length */ 1.714 + } 1.715 + 1.716 + if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ 1.717 + entropy->EOBRUN++; /* count an EOB */ 1.718 + entropy->BE += BR; /* concat my correction bits to older ones */ 1.719 + /* We force out the EOB if we risk either: 1.720 + * 1. overflow of the EOB counter; 1.721 + * 2. overflow of the correction bit buffer during the next MCU. 1.722 + */ 1.723 + if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) 1.724 + emit_eobrun(entropy); 1.725 + } 1.726 + 1.727 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.728 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.729 + 1.730 + /* Update restart-interval state too */ 1.731 + if (cinfo->restart_interval) { 1.732 + if (entropy->restarts_to_go == 0) { 1.733 + entropy->restarts_to_go = cinfo->restart_interval; 1.734 + entropy->next_restart_num++; 1.735 + entropy->next_restart_num &= 7; 1.736 + } 1.737 + entropy->restarts_to_go--; 1.738 + } 1.739 + 1.740 + return TRUE; 1.741 +} 1.742 + 1.743 + 1.744 +/* 1.745 + * Finish up at the end of a Huffman-compressed progressive scan. 1.746 + */ 1.747 + 1.748 +METHODDEF(void) 1.749 +finish_pass_phuff (j_compress_ptr cinfo) 1.750 +{ 1.751 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.752 + 1.753 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.754 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.755 + 1.756 + /* Flush out any buffered data */ 1.757 + emit_eobrun(entropy); 1.758 + flush_bits(entropy); 1.759 + 1.760 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.761 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.762 +} 1.763 + 1.764 + 1.765 +/* 1.766 + * Finish up a statistics-gathering pass and create the new Huffman tables. 1.767 + */ 1.768 + 1.769 +METHODDEF(void) 1.770 +finish_pass_gather_phuff (j_compress_ptr cinfo) 1.771 +{ 1.772 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.773 + boolean is_DC_band; 1.774 + int ci, tbl; 1.775 + jpeg_component_info * compptr; 1.776 + JHUFF_TBL **htblptr; 1.777 + boolean did[NUM_HUFF_TBLS]; 1.778 + 1.779 + /* Flush out buffered data (all we care about is counting the EOB symbol) */ 1.780 + emit_eobrun(entropy); 1.781 + 1.782 + is_DC_band = (cinfo->Ss == 0); 1.783 + 1.784 + /* It's important not to apply jpeg_gen_optimal_table more than once 1.785 + * per table, because it clobbers the input frequency counts! 1.786 + */ 1.787 + MEMZERO(did, SIZEOF(did)); 1.788 + 1.789 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.790 + compptr = cinfo->cur_comp_info[ci]; 1.791 + if (is_DC_band) { 1.792 + if (cinfo->Ah != 0) /* DC refinement needs no table */ 1.793 + continue; 1.794 + tbl = compptr->dc_tbl_no; 1.795 + } else { 1.796 + tbl = compptr->ac_tbl_no; 1.797 + } 1.798 + if (! did[tbl]) { 1.799 + if (is_DC_band) 1.800 + htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; 1.801 + else 1.802 + htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; 1.803 + if (*htblptr == NULL) 1.804 + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 1.805 + jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); 1.806 + did[tbl] = TRUE; 1.807 + } 1.808 + } 1.809 +} 1.810 + 1.811 + 1.812 +/* 1.813 + * Module initialization routine for progressive Huffman entropy encoding. 1.814 + */ 1.815 + 1.816 +GLOBAL(void) 1.817 +jinit_phuff_encoder (j_compress_ptr cinfo) 1.818 +{ 1.819 + phuff_entropy_ptr entropy; 1.820 + int i; 1.821 + 1.822 + entropy = (phuff_entropy_ptr) 1.823 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.824 + SIZEOF(phuff_entropy_encoder)); 1.825 + cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; 1.826 + entropy->pub.start_pass = start_pass_phuff; 1.827 + 1.828 + /* Mark tables unallocated */ 1.829 + for (i = 0; i < NUM_HUFF_TBLS; i++) { 1.830 + entropy->derived_tbls[i] = NULL; 1.831 + entropy->count_ptrs[i] = NULL; 1.832 + } 1.833 + entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ 1.834 +} 1.835 + 1.836 +#endif /* C_PROGRESSIVE_SUPPORTED */