一、实验原理
1.jpg编码
JPEG(Joint Photographic Experts Group),文件后缀名为.jpg,.jpeg,是一种常用的图像文件格式。采用有损压缩方式去除冗余的图像和彩色数据,在获得极高压缩率的同时能展现十分丰富生动的图像。
编码过程如图示:
解码为编码的逆过程。
2.jpg文件格式
jpeg文件以Segment的形式组织,均以0xFF开始,后面跟1字节的Marker和2字节的Segment Length(包含Length本身长度,不包括Marker和0xFF)。
常用的Marker有:
1)SOI(start of image)0xFFDB
所有的jpeg文件必须以SOI开始。
2)APPn(reserved for application use)0xFFE0-0xFFEF
eg:FFE0 运用程序保留标记0
FF E0 00 10 4A 46 49 46 00 01 01 01 00 48 00 48 00 00
length 2byte:0X0010=16
标识符:4A 46 49 46 00 –JFIF
Version 2byte:0101
Units 1byte:01 –X,Y are dots per inch
Xdensity 2byte:0x0048 –水平方向点密度
Ydensity 2byte:0x0048 –垂直方向点密度
缩略图水平像素数目 1byte :0x00
缩略图垂直像素数目 1byte :0x00
缩略图24bitRGB 数据:没有缩略图
3)DQT(Define Quantization Table) 0xFFDB
length 2byte:0x 00 43
QT信息 1byte 0-3位:QT号(0-3,可能有4张量化表,亮度,色度,可能会有rgb各一张量化表) 4-7位:QT精度(0:8bit,1:16bit)
QT实际数据:nbyte n=64*QT精度的字节数。(对于8*8的宏块来说,共有8*8=64个量化系数)
需要注意的是,jpeg文件中的量化表是按之字形扫描的格式存的,要得到真正的量化表还需要反之字形扫描;
static const unsigned char zigzag[64] =
{
0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63
};
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;
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
}
}
}
解析到的量化表为