0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 0N/A * Copyright (C) 1995-1997, Thomas G. Lane. 0N/A * This file is part of the Independent JPEG Group's software. 0N/A * For conditions of distribution and use, see the accompanying README file. 0N/A * This file contains Huffman entropy encoding routines for progressive JPEG. 0N/A * We do not support output suspension in this module, since the library 0N/A * currently does not allow multiple-scan files to be written with output 0N/A/* Expanded entropy encoder object for progressive Huffman encoding. */ 0N/A /* Mode flag: TRUE for optimization, FALSE for actual data output */ 0N/A /* Bit-level coding status. 0N/A /* Coding status for DC components */ 0N/A /* Coding status for AC components */ 0N/A int ac_tbl_no;
/* the table number of the single component */ 0N/A unsigned int EOBRUN;
/* run length of EOBs */ 0N/A unsigned int BE;
/* # of buffered correction bits before MCU */ 0N/A /* packing correction bits tightly would save some space but cost time... */ 0N/A /* Pointers to derived tables (these workspaces have image lifespan). 0N/A * Since any one scan codes only DC or only AC, we only need one set 0N/A * of tables, not one for DC and one for AC. 0N/A /* Statistics tables for optimization; again, one set is enough */ 0N/A/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit 0N/A * buffer can hold. Larger sizes may slightly improve compression, but 0N/A * 1000 is already well into the realm of overkill. 0N/A * The minimum safe size is 64 bits. 0N/A/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. 0N/A * We assume that int right shift is unsigned if INT32 right shift is, 0N/A * which should be safe. 0N/A/* Forward declarations */ 0N/A * Initialize for a Huffman-compressed scan using progressive JPEG. 0N/A /* We assume jcmaster.c already validated the scan parameters. */ 0N/A /* Select execution routines */ 0N/A /* AC refinement needs a correction bit buffer */ 0N/A /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 0N/A * for AC coefficients. 0N/A /* Initialize DC predictions to 0 */ 0N/A /* Get table index */ 0N/A if (
cinfo->
Ah != 0)
/* DC refinement needs no table */ 0N/A /* Check for invalid table index */ 0N/A /* (make_c_derived_tbl does this in the other path) */ 0N/A /* Allocate and zero the statistics tables */ 0N/A /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ 0N/A /* Compute derived values for Huffman table */ 0N/A /* We may do this more than once for a table, but it's not expensive */ 0N/A /* Initialize AC stuff */ 0N/A /* Initialize bit buffer to empty */ 0N/A /* Initialize restart stuff */ 0N/A/* Outputting bytes to the file. 0N/A * NB: these must be called only when actually outputting, 0N/A * that is, entropy->gather_statistics == FALSE. 0N/A/* Empty the output buffer; we do not support suspension in this module. */ 0N/A /* After a successful buffer dump, must reset buffer pointers */ 0N/A/* Outputting bits to the file */ 0N/A/* Only the right 24 bits of put_buffer are used; the valid bits are 0N/A * left-justified in this part. At most 16 bits can be passed to emit_bits 0N/A * in one call, and we never retain more than 7 bits in put_buffer 0N/A * between calls, so 24 bits are sufficient. 0N/A/* Emit some bits, unless we are in gather mode */ 0N/A /* This routine is heavily used, so it's worth coding tightly. */ 0N/A /* if size is 0, caller used an invalid Huffman table entry */ 0N/A return;
/* do nothing if we're only getting stats */ 0N/A if (c ==
0xFF) {
/* need to stuff a zero byte? */ 0N/A * Emit (or just count) a Huffman symbol. 0N/A * Emit bits from a correction bit buffer. 0N/A return;
/* no real work */ 0N/A * Emit any pending EOBRUN symbol. 0N/A /* safety check: shouldn't happen given limited correction-bit buffer */ 0N/A /* Emit any buffered correction bits */ 0N/A * Emit a restart marker & resynchronize predictions. 0N/A /* Re-initialize DC predictions to 0 */ 0N/A /* Re-initialize all AC-related fields to 0 */ 0N/A * MCU encoding for DC initial scan (either spectral selection, 0N/A * or first pass of successive approximation). 0N/A /* Emit restart marker if needed */ 0N/A /* Encode the MCU data blocks */ 0N/A /* Compute the DC value after the required point transform by Al. 0N/A * This is simply an arithmetic right shift. 0N/A /* DC differences are figured on the point-transformed values. */ 0N/A /* Encode the DC coefficient difference per section G.1.2.1 */ 0N/A /* For a negative input, want temp2 = bitwise complement of abs(input) */ 0N/A /* This code assumes we are on a two's complement machine */ 0N/A /* Find the number of bits needed for the magnitude of the coefficient */ 0N/A /* Check for out-of-range coefficient values. 0N/A * Since we're encoding a difference, the range limit is twice as much. 0N/A /* Count/emit the Huffman-coded symbol for the number of bits */ 0N/A /* Emit that number of bits of the value, if positive, */ 0N/A /* or the complement of its magnitude, if negative. */ 0N/A if (
nbits)
/* emit_bits rejects calls with size 0 */ 0N/A /* Update restart-interval state too */ 0N/A * MCU encoding for AC initial scan (either spectral selection, 0N/A * or first pass of successive approximation). 0N/A /* Emit restart marker if needed */ 0N/A /* Encode the MCU data block */ 0N/A /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ 0N/A r = 0;
/* r = run length of zeros */ 0N/A /* We must apply the point transform by Al. For AC coefficients this 0N/A * is an integer division with rounding towards 0. To do this portably 0N/A * in C, we shift after obtaining the absolute value; so the code is 0N/A * interwoven with finding the abs value (temp) and output bits (temp2). 0N/A /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ 0N/A /* Watch out for case that nonzero coef is zero after point transform */ 0N/A /* Emit any pending EOBRUN */ 0N/A /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 0N/A /* Find the number of bits needed for the magnitude of the coefficient */ 0N/A nbits =
1;
/* there must be at least one 1 bit */ 0N/A /* Check for out-of-range coefficient values */ 0N/A /* Count/emit Huffman symbol for run length / number of bits */ 0N/A /* Emit that number of bits of the value, if positive, */ 0N/A /* or the complement of its magnitude, if negative. */ 0N/A r = 0;
/* reset zero run length */ 0N/A if (r > 0) {
/* If there are trailing zeroes, */ 0N/A /* Update restart-interval state too */ 0N/A * MCU encoding for DC successive approximation refinement scan. 0N/A * Note: we assume such scans can be multi-component, although the spec 0N/A * is not very clear on the point. 0N/A /* Emit restart marker if needed */ 0N/A /* Encode the MCU data blocks */ 0N/A /* We simply emit the Al'th bit of the DC coefficient value. */ 0N/A /* Update restart-interval state too */ 0N/A * MCU encoding for AC successive approximation refinement scan. 0N/A /* Emit restart marker if needed */ 0N/A /* Encode the MCU data block */ 0N/A /* It is convenient to make a pre-pass to determine the transformed 0N/A * coefficients' absolute values and the EOB position. 0N/A /* We must apply the point transform by Al. For AC coefficients this 0N/A * is an integer division with rounding towards 0. To do this portably 0N/A * in C, we shift after obtaining the absolute value. 0N/A EOB = k;
/* EOB = index of last newly-nonzero coef */ 0N/A /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ 0N/A r = 0;
/* r = run length of zeros */ 0N/A BR = 0;
/* BR = count of buffered bits added now */ 0N/A /* Emit any required ZRLs, but not if they can be folded into EOB */ 0N/A /* emit any pending EOBRUN and the BE correction bits */ 0N/A /* Emit buffered correction bits that must be associated with ZRL */ 0N/A /* If the coef was previously nonzero, it only needs a correction bit. 0N/A * NOTE: a straight translation of the spec's figure G.7 would suggest 0N/A * that we also need to test r > 15. But if r > 15, we can only get here 0N/A * if k > EOB, which implies that this coefficient is not 1. 0N/A /* The correction bit is the next bit of the absolute value. */ 0N/A /* Emit any pending EOBRUN and the BE correction bits */ 0N/A /* Count/emit Huffman symbol for run length / number of bits */ 0N/A /* Emit output bit for newly-nonzero coef */ 0N/A /* Emit buffered correction bits that must be associated with this code */ 0N/A r = 0;
/* reset zero run length */ 0N/A if (r > 0 ||
BR > 0) {
/* If there are trailing zeroes, */ 0N/A /* We force out the EOB if we risk either: 0N/A * 1. overflow of the EOB counter; 0N/A * 2. overflow of the correction bit buffer during the next MCU. 0N/A /* Update restart-interval state too */ 0N/A * Finish up at the end of a Huffman-compressed progressive scan. 0N/A /* Flush out any buffered data */ 0N/A * Finish up a statistics-gathering pass and create the new Huffman tables. 0N/A /* Flush out buffered data (all we care about is counting the EOB symbol) */ 0N/A /* It's important not to apply jpeg_gen_optimal_table more than once 0N/A * per table, because it clobbers the input frequency counts! 0N/A if (
cinfo->
Ah != 0)
/* DC refinement needs no table */ 0N/A * Module initialization routine for progressive Huffman entropy encoding. 0N/A /* Mark tables unallocated */ 0N/A#
endif /* C_PROGRESSIVE_SUPPORTED */