C# Class BitMiracle.LibJpeg.Classic.Internal.huff_entropy_encoder

Expanded entropy encoder object for Huffman encoding.
Inheritance: jpeg_entropy_encoder
Show file Open project: prepare/HTML-Renderer

Public Methods

Method Description
huff_entropy_encoder ( jpeg_compress_struct cinfo ) : System
start_pass ( bool gather_statistics ) : void

Initialize for a Huffman-compressed scan. If gather_statistics is true, we do not output anything during the scan, just count the Huffman symbols used and generate Huffman code tables.

Protected Methods

Method Description
jpeg_gen_optimal_table ( JHUFF_TBL htbl, long freq ) : void

Generate the best Huffman code table for the given counts, fill htbl. The JPEG standard requires that no symbol be assigned a codeword of all one bits (so that padding bits added at the end of a compressed segment can't look like a valid code). Because of the canonical ordering of codewords, this just means that there must be an unused slot in the longest codeword length category. Section K.2 of the JPEG spec suggests reserving such a slot by pretending that symbol 256 is a valid symbol with count 1. In theory that's not optimal; giving it count zero but including it in the symbol set anyway should give a better Huffman code. But the theoretically better code actually seems to come out worse in practice, because it produces more all-ones bytes (which incur stuffed zero bytes in the final file). In any case the difference is tiny. The JPEG standard requires Huffman codes to be no more than 16 bits long. If some symbols have a very small but nonzero probability, the Huffman tree must be adjusted to meet the code length restriction. We currently use the adjustment method suggested in JPEG section K.2. This method is *not* optimal; it may not choose the best possible limited-length code. But typically only very-low-frequency symbols will be given less-than-optimal lengths, so the code is almost optimal. Experimental comparisons against an optimal limited-length-code algorithm indicate that the difference is microscopic --- usually less than a hundredth of a percent of total size. So the extra complexity of an optimal algorithm doesn't seem worthwhile.

Private Methods

Method Description
IRIGHT_SHIFT ( int x, int shft ) : int

IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than int. We assume that int right shift is unsigned if int right shift is, which should be safe.

dump_buffer_e ( ) : bool
dump_buffer_s ( ) : bool
emit_ac_symbol ( int tbl_no, int symbol ) : void
emit_bits_e ( int code, int size ) : void

Outputting bits to the file Only the right 24 bits of put_buffer are used; the valid bits are left-justified in this part. At most 16 bits can be passed to emit_bits in one call, and we never retain more than 7 bits in put_buffer between calls, so 24 bits are sufficient.

emit_bits_s ( savable_state state, int code, int size ) : bool

Only the right 24 bits of put_buffer are used; the valid bits are left-justified in this part. At most 16 bits can be passed to emit_bits in one call, and we never retain more than 7 bits in put_buffer between calls, so 24 bits are sufficient.

emit_buffered_bits ( uint offset, uint nbits ) : void
emit_byte_e ( int val ) : void
emit_byte_s ( int val ) : bool
emit_dc_symbol ( int tbl_no, int symbol ) : void
emit_eobrun ( ) : void
emit_restart_e ( int restart_num ) : void
emit_restart_s ( savable_state state, int restart_num ) : bool

Emit a restart marker and resynchronize predictions.

encode_mcu_AC_first ( JBLOCK MCU_data ) : bool

MCU encoding for AC initial scan (either spectral selection, or first pass of successive approximation).

encode_mcu_AC_refine ( JBLOCK MCU_data ) : bool

MCU encoding for AC successive approximation refinement scan.

encode_mcu_DC_first ( JBLOCK MCU_data ) : bool

MCU encoding for DC initial scan (either spectral selection, or first pass of successive approximation).

encode_mcu_DC_refine ( JBLOCK MCU_data ) : bool

MCU encoding for DC successive approximation refinement scan. Note: we assume such scans can be multi-component, although the spec is not very clear on the point.

encode_mcu_gather ( JBLOCK MCU_data ) : bool

Trial-encode one MCU's worth of Huffman-compressed coefficients. No data is actually output, so no suspension return is possible.

encode_mcu_huff ( JBLOCK MCU_data ) : bool

Encode and output one MCU's worth of Huffman-compressed coefficients.

encode_one_block ( savable_state state, short block, int last_dc_val, c_derived_tbl dctbl, c_derived_tbl actbl ) : bool

Encode a single block's worth of coefficients

finish_pass_gather ( ) : void

Finish up a statistics-gathering pass and create the new Huffman tables.

finish_pass_huff ( ) : void

Finish up at the end of a Huffman-compressed scan.

flush_bits_e ( ) : void
flush_bits_s ( savable_state state ) : bool
htest_one_block ( short block, int last_dc_val, long dc_counts, long ac_counts ) : void

Huffman coding optimization. We first scan the supplied data and count the number of uses of each symbol that is to be Huffman-coded. (This process MUST agree with the code above.) Then we build a Huffman coding tree for the observed counts. Symbols which are not needed at all for the particular image are not assigned any code, which saves space in the DHT marker as well as in the compressed data.

jpeg_make_c_derived_tbl ( bool isDC, int tblno, c_derived_tbl &dtbl ) : void

Expand a Huffman table definition into the derived format Compute the derived values for a Huffman table. This routine also performs some validation checks on the table.

Method Details

huff_entropy_encoder() public method

public huff_entropy_encoder ( jpeg_compress_struct cinfo ) : System
cinfo jpeg_compress_struct
return System

jpeg_gen_optimal_table() protected method

Generate the best Huffman code table for the given counts, fill htbl. The JPEG standard requires that no symbol be assigned a codeword of all one bits (so that padding bits added at the end of a compressed segment can't look like a valid code). Because of the canonical ordering of codewords, this just means that there must be an unused slot in the longest codeword length category. Section K.2 of the JPEG spec suggests reserving such a slot by pretending that symbol 256 is a valid symbol with count 1. In theory that's not optimal; giving it count zero but including it in the symbol set anyway should give a better Huffman code. But the theoretically better code actually seems to come out worse in practice, because it produces more all-ones bytes (which incur stuffed zero bytes in the final file). In any case the difference is tiny. The JPEG standard requires Huffman codes to be no more than 16 bits long. If some symbols have a very small but nonzero probability, the Huffman tree must be adjusted to meet the code length restriction. We currently use the adjustment method suggested in JPEG section K.2. This method is *not* optimal; it may not choose the best possible limited-length code. But typically only very-low-frequency symbols will be given less-than-optimal lengths, so the code is almost optimal. Experimental comparisons against an optimal limited-length-code algorithm indicate that the difference is microscopic --- usually less than a hundredth of a percent of total size. So the extra complexity of an optimal algorithm doesn't seem worthwhile.
protected jpeg_gen_optimal_table ( JHUFF_TBL htbl, long freq ) : void
htbl JHUFF_TBL
freq long
return void

start_pass() public method

Initialize for a Huffman-compressed scan. If gather_statistics is true, we do not output anything during the scan, just count the Huffman symbols used and generate Huffman code tables.
public start_pass ( bool gather_statistics ) : void
gather_statistics bool
return void