实验原理
可知jpeg编码的步骤如上图所示 下面具体分析每一项
1.rgb到yuv,目的是减少冗余,yuv的关联性较rgb小,电平偏移,减少数据的范围
2.将图像切割分为8*8的块,方便进行dct变化
3.dct变化,将图像放到频域上,左上角是直流分量,向右下角频率增高,此步骤可以去除空间冗余,方面后面用不同的量化步长进行量化
4.量化,采用不同步长的量化
5.熵编码,直流采取哈夫曼编码,交流采取游程编码
重点码表介绍
重点码表包括量化表和哈夫曼表
量化表的表头是FF DB 后面两字节xx xx 表示长度 后面字节xx高四位表示精度 低四位表示表号
注意量化表采用zig-zag排列
哈夫曼码表包括直流码表和交流码表
FF C4 后面两字节 xx xx表示数据长度 再后面1字节为 Huffman Table 的信息 低4位是 HT ID 号 第5位是 HT 表类型标记
代码分析
1存储哈夫曼表
struct huffman_table
{
/* Fast look up table, using HUFFMAN_HASH_NBITS bits we can have directly the symbol,
* if the symbol is <0, then we need to look into the tree table */
short int lookup[HUFFMAN_HASH_SIZE];
/* code size: give the number of bits of a symbol is encoded */
unsigned char code_size[HUFFMAN_HASH_SIZE];
uint16_t slowtable[16-HUFFMAN_HASH_NBITS][256];
};
2.存储JPEG图像宽高,指针,码表等
struct jdec_private
{
/* Public variables */
uint8_t *components[COMPONENTS];
unsigned int width, height; /* Size of the image */
unsigned int flags;
/* Private variables */
const unsigned char *stream_begin, *stream_end;
unsigned int stream_length;
const unsigned char *stream; /* Pointer to the current stream */
unsigned int reservoir, nbits_in_reservoir;
struct component component_infos[COMPONENTS];
float Q_tables[COMPONENTS