一.实验目的
掌握JPEG编解码系统的基本原理。初步掌握复杂的数据压缩算法实现,并能根据理论分析需要实现所对应数据的输出。
二.实验原理
1.JPEG编解码原理
JPEG编码的过程如上图所示。解码是编码的逆过程。
2.JPEG文件格式
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字节。
三.实验步骤
1.逐步调试JPEG解码器程序。将输入的JPG文件进行解码,将输出文件保存为可供YUVViewer观看的YUV文件。
2. 程序调试过程中,应做到:
理解程序设计的整体框架
理解三个结构体的设计目的
• struct huffman_table
• struct component
• struct jdec_private
理解在视音频编解码调试中TRACE的目的和含义
• 会打开和关闭TRACE
• 会根据自己的要求修改TRACE
3.以txt文件输出所有的量化矩阵和所有的HUFFMAN码表。
4.输出DC图像并统计其概率分布。
5.输出某一个AC值图像并统计其概率分布。
四.代码
1.yuv代码添加
static void write_yuv(const char *filename, int width, int height, unsigned char **components)
{
FILE *F;
char temp[1024];
snprintf(temp, 1024, "%s.Y", filename);
F = fopen(temp, "wb");
fwrite(components[0], width, height, F);
fclose(F);
snprintf(temp, 1024, "%s.U", filename);
F = fopen(temp, "wb");
fwrite(components[1], width*height/4, 1, F);
fclose(F);
snprintf(temp, 1024, "%s.V", filename);
F = fopen(temp, "wb");
fwrite(components[2], width*height/4, 1, F);
fclose(F);
//添加部分
snprintf(temp, 1024, "%s.YUV", filename);
F = fopen(temp, "wb");
fwrite(components[0], width* height, 1,F);
fwrite(components[1], width * height / 4, 1, F);
fwrite(components[2], width * height / 4, 1, F);
fclose(F);
}
原图:
yuv输出:
2.量化矩阵代码:
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
{
/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
* For float AA&N IDCT method, divisors are equal to quantization
* coefficients scaled by scalefactor[row]*scalefactor[col], where
* scalefactor[0] = 1
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
* We apply a further scale factor of 8.
* What's actually stored is 1/divisor so that the inner loop can
* use a multiplication rather than a division.
*/
int i, j;
static const double aanscalefactor[8] = {
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
};
const unsigned char *zz = zigzag;
//输出量化矩阵
#if TRACE
for(i=0;i<8;i++){
for(j=0;j<8;j++){
fprintf(p_trace,"%6d",ref_table[*zz++]);
}
fprintf(p_trace,"\n");
}
#endif
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
}
}
}
输出txt文件:
3.输出huffman码表(代码已有):