📄 huffman_tbl.c
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////////// huffman_tbl.c (Huffman table functions 'C' File)//// Notice: COPYRIGHT (C) STREAM PROCESSORS, INC. 2005-2007// THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE SPI// END-USER LICENSE AGREEMENT (EULA). THE PROGRAM MAY ONLY// BE USED IN A MANNER EXPLICITLY SPECIFIED IN THE EULA,// WHICH INCLUDES LIMITATIONS ON COPYING, MODIFYING,// REDISTRIBUTION AND WARANTIES. UNAUTHORIZED USE OF THIS// PROGRAM IS STRICTLY PROHIBITED. YOU MAY OBTAIN A COPY OF// THE EULA FROM WWW.STREAMPROCESSORS.COM. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #includes ////////////////////////////////////////////////////////////////////////////////////////////////////#include <string.h>#include "spi_common.h"#include "jpege_context.h"/* * Compute the derived values for a Huffman table. * This routine also performs some validation checks on the table. * * Note this is also used by jcphuff.c. */void derive_huffman_table( JPEGE_HUFF_TBL *p_huff_tbl, DERIVED_HUFF_TBL *p_d_huff_tbl){ int k, i, j; int count; int lastk, si; char huffsize[257]; unsigned short huffcode[257]; unsigned short code; /* Note that huffsize[] and huffcode[] are filled in code-length order, * paralleling the order of the symbols themselves in htbl->huffval[]. */ // Figure C.1: make table of Huffman code length for each symbol (huffsize[]) k = 0; for (i = 1; i <= 16; i++) { count = (int) p_huff_tbl->bits[i]; for (j = 0; j < count; j++) { huffsize[k++] = (char) i; } } huffsize[k] = 0; lastk = k; // Figure C.2: generate the codes themselves (huffcode[]) // We also validate that the counts represent a legal Huffman code tree. code = 0; si = huffsize[0]; k = 0; while (huffsize[k]) { while (((int) huffsize[k]) == si) { huffcode[k++] = code; code++; } code <<= 1; si++; } // Figure C.3: generate encoding tables // These are code and size indexed by symbol value // Set all codeless symbols to have code length 0; // this lets us detect duplicate VAL entries here, and later // allows emit_bits to detect any attempt to emit such symbols. memset(p_d_huff_tbl->code_length, 0, sizeof (p_d_huff_tbl->code_length)); /* This is also a convenient place to check for out-of-range * and duplicated VAL entries. We allow 0..255 for AC symbols * but only 0..15 for DC. (We could constrain them further * based on data depth and mode, but this seems enough.) */ //maxsymbol = isDC ? 15 : 255; for (k = 0; k < lastk; k++) { i = p_huff_tbl->huffval[k]; p_d_huff_tbl->code_word[i] = huffcode[k]; p_d_huff_tbl->code_length[i] = huffsize[k]; } p_huff_tbl->table_sent = false;}void set_huffman_table (JPEGE_HUFF_TBL *p_huff_table, unsigned char *p_bits, unsigned char *p_values){ int i; int num_symbols; SPI_ASSERT (p_huff_table != NULL); SPI_ASSERT (p_bits != NULL); SPI_ASSERT (p_values != NULL); // check that number of symbols are within range. num_symbols = 0; for (i = 0; i <= 16; i++) { num_symbols += p_bits[i]; p_huff_table->bits[i] = p_bits[i]; } if (num_symbols < 1 || num_symbols > MAX_NUM_HUFF_SYMBOL) { spi_printf ("huffman table is out of range\n"); exit (1); // TODO, better error handling } for (i = 0; i < num_symbols; i++) { p_huff_table->huffval[i] = p_values[i]; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -