一、实验目的
掌握JPEG编解码系统的基本原理。初步掌握复杂的数据压缩算法实现,并能根据理论分析需要实现所对应数据的输出。
二、实验原理
(一)JPGE文件格式:
1.文件格式:
SOI:Start of Image 图像开始;
APP0:Application 应用程序保留标记0;
DQT:Define Quantization Table 定义量化表;
SOF0:Start of Frame 帧图像开始;
DHT:Define Huffman Table 定义哈夫曼表;
SOS:Start of Scan 扫描开始12字节;
EOI:End of Image 图像结束2字节。
2.解码流程:
①读入文件的相关信息;
②初步了解图像数据流的结构;
③颜色分量单元的内部解码;
④直流系数的差分编码;
⑤反量化&反Zig-zag编码;
⑥反离散余弦变换。
3.解码程序实现:HUffman查找表实现
两种主要方法:①Huffman树的遍历;②lookup查找表
(二)JPGE编解码原理
JPEG编码的过程如上图所示。解码是编码的逆过程。
三、实验要求
1.逐步调试JPEG解码器程序。将输入的JPG文件进行解码,将输出文件保存为可供YUVViewer观看的YUV文件。
2.程序调试过程中,应做到:
① 理解程序设计的整体框架
② 理解三个结构体的设计目的
• struct huffman_table
• struct component
• struct jdec_private
③ 理解在视音频编解码调试中TRACE的目的和含义
• 会打开和关闭TRACE
• 会根据自己的要求修改TRACE
3.以txt文件输出所有的量化矩阵和所有的HUFFMAN码表。
4. 输出DC图像并统计其概率分布。
5. 输出某一个AC值图像并统计其概率分布。
四、代码实现
Huffman解码过程:
static int get_next_huffman_code(struct jdec_private* priv, struct huffman_table* huffman_table)
{
int value, hcode;
unsigned int extra_nbits, nbits;
uint16_t* slowtable;
//读取HUFFMAN_HASH_NBITS比特,并赋值给hcode
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
//在霍夫曼码表中找到对应的权值
value = huffman_table->lookup[hcode];
if (__likely(value >= 0))
{
unsigned int code_size = huffman_table->code_size[value];
//跳过code_size比特
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
//返回权值
return value;
}
/* Decode more bits each time ...每次读取更多bit */
for (extra_nbits = 0; extra_nbits < 16 - HUFFMAN_HASH_NBITS; extra_nbits++)
{
nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
//读取n比特
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
slowtable = huffman_table->slowtable[extra_nbits];
/* Search if the code is in this array */
while (slowtable[0]) {
if (slowtable[0] == hcode) {
//如果找到,那么将当前的bit数跳过并返回权值
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
return slowtable[1];
}
slowtable += 2;//查找下一个位置有没有
}
}
return 0;
}
加载一个jpeg图像,对其进行解压缩,并存储结果
/**
* Load one jpeg image, and decompress it, and save the result.
* 加载一个jpeg图像,对其进行解压缩,并存储结果
*/
int convert_one_image(const char *infilename, const char *outfilename, int output_format)
{
FILE *fp;
unsigned int length_of_file;
unsigned int width, height;
unsigned char *buf;
struct jdec_private *jdec;
unsigned char *components[3];
/* Load the Jpeg into memory */
fp = fopen(infilename, "rb");
if (fp == NULL)
exitmessage("Cannot open filename\n");
length_of_file = filesize(fp);
buf = (unsigned char *)malloc(length_of_file + 4);
if (buf == NULL)
exitmessage("Not enough memory for loading file\n");
fread(buf, length_of_file, 1, fp);
fclose(fp);
/* Decompress it 初始化解码器*/
jdec = tinyjpeg_init();
if (jdec == NULL)
exitmessage("Not enough memory to alloc the structure need for decompressing\n");
//对header进行解码
if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
exitmessage(tinyjpeg_get_errorstring(jdec));
/* Get the size of the image 获取图像大小*/
tinyjpeg_get_size(jdec, &width, &height);
snprintf(