一.BMP文件格式介绍
位图文件格式(BMP)是Windows采用的图像文件存储格式,在Windows环境下运行的所有图像处理软件都支持这种格式。
BMP文本大体上分为四个部分:
部分 | 说明 |
---|---|
位图文件头BITMAPFILEHEADER | 包含BMP图像文件的类型,显示内容等信息 |
位图信息头BITMAPINFOHEADER | 包含有BMP图像的宽,高压缩方法,以及定义颜色等信息 |
调色板 Palette | 可选,真彩(24BMP)不需要调色板 |
实际的位图数据 ImageData | 根据位图使用的位数不同而不同,在24位图中直接使用RGB,而其他的小于24位的使用颜色板中索引值 |
1.位图文件头
typedef struct tagBITMAPFILEHEADER {
WORD bfType;//说明文件的类型
DWORD bfSize;//说明文件的大小,以字节为单位
WORD bfReserved1;//保留,设置为0
WORD bfReserved2;//保留,设置为0
DWORD bfOffBits;//说明从BITMAPFILEHEADER结构开始到实际的图像数据之间的字节偏移量
}BITMAPFILEHEADER;
2.位图信息头
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;//说明结构体所需字节数
LONG biWidth;//以像素为单位说明图像的宽度,对于32为的机子来说,LONG也是4字节,只是有一位被用来做符号位
LONG biHeight;//以像素为单位说明图像的高度
WORD biPlanes;//说明位面数,必须为1
WORD biBitCount;//说明位数每像素,1,2,4,8,24
DWORD biCompression;//说明图像是否压缩及压缩类型
DWORD biSizeImage;//以字节为单位说明图像大小,必须是4的整数倍
LONG biXPelsPerMeter;//目标设备的水平分辨率,像素每米
LONG biYPelsPerMeter;//目标设备的垂直分辨率,像素每米
DWORD biClrUsed;//说明图像实际用到的颜色数,如果为0,则颜色数为2的biBitCount次方
DWORD biClrImportant;//说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要
}BITMAPINFOHEADER;
3.调色板
typedef struct tagRGBQUAD {
BYTE rgbBlue;//指定蓝色分量
BYTE rgbGreen;//指定绿色分量
BYTE rgbRed;//指定红色分量
BYTE rgbReserved;//保留,指定为0
}RGBQUAD;
使用FlexHEX打开实验所需BMP文件:
文件头部分:
“42 4D”:说明文件类型为BMP。
“66 75 00 00”:(小端存储,0x“00007566”)说明文件大小为30054字节。
“36 00 00 00”: (小端存储,0x“00000036”)说明从结构体开始到实际的图像数据之间偏移量为54字节。
信息头部分:
“28 00 00 00”:(0x"00000028"),说明结构体所需字节数为40字节
“64 00 00 00”:(0x"00000064"),说明图像的宽度为100像素
“64 00 00 00”:(0x"00000064"),说明图像的高度为100像素
“01 00”:(0x"0001"),位面数,必须为1
“18 00”:(0x"0018"),说明24位/像素
“13 0B 00 00”:(0x"00000B13"),说明目标设备的水平分辨率为2835
“13 0B 00 00”:(0x"00000B13"),说明目标设备的垂直分辨率为2835
由此,我们知道了该实验图片的基本信息。
二.实验流程
1.程序初始化(打开两个文件,定义变量和缓冲区等)
2.读取BMP文件,抽取或生成RGB数据写入缓冲区
3.调用RGB2YUV的函数实现RGB到YUV数据的转换
4.写YUV文件
5.程序收尾工作(关闭文件,释放缓冲区)
三. 实验原理及代码
1.头文件BMP2YUV.h
头文件进行函数声明,方便函数调用
#pragma once
#include<windows.h>
#ifndef BMP2YUV_H_
#define BMP2YUV_H_
void BMP2RGB(int width, int height, FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, unsigned char* rgbout);
bool MakePalette(FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, RGBQUAD* rgbout);
void RGB2YUV(int width, int height, unsigned char* rgbbuf, unsigned char* ybuf, unsigned char* ubuf, unsigned char* vbuf);
void DownYUV(int width, int height, unsigned char* utmp, unsigned char* vtmp, unsigned char* ubuf, unsigned char* vbuf);
#endif
2.main.cpp
(1)程序初始化,定义变量和缓冲区
FILE* bmpfile = NULL, * yuvfile = NULL;
BITMAPFILEHEADER file_header;
BITMAPINFOHEADER info_header;
unsigned char* rgbbuf = NULL;
unsigned char* ybuf = NULL;
unsigned char* ubuf = NULL;
unsigned char* vbuf = NULL;
int width, height;
(2)定义输入输出:
main()函数带有参数argc和argv,argc表示输入变量的个数,argv[0]为生成的.exe文件,后面依次为输入参数。
本实验输入参数分别为五个bmp文件的文件名以及重复次数。在项目属性-调试-命令参数中,可以提前设置好输入参数。
int repeat_num;
const char* bmpfilename[5] = {0};
const char* yuvfilename;
bmpfilename[0] = argv[1];
bmpfilename[1] = argv[2];
bmpfilename[2] = argv[3];
bmpfilename[3] = argv[4];
bmpfilename[4] = argv[5];
repeat_num = atoi(argv[6]);
yuvfilename = "out.yuv";
(3)读位图文件头及位图信息头
for (int k = 0; k < 5; k++)
{
//判断文件是否能打开
if ((bmpfile = fopen(bmpfilename[k], "rb")) == NULL)
{
printf("can't find bmpfilie");
exit(0);
}
if ((yuvfile = fopen(yuvfilename, "ab")) == NULL)
{
printf("can't find yuvfilie");
exit(0);
}
//读取文件头
if (fread(&file_header, sizeof(BITMAPFILEHEADER), 1, bmpfile) != 1)
{
printf("read file header error!");
exit(0);
}
//判断是否为BMP文件
if (file_header.bfType != 0x4D42)
{
printf("not bmp file");
exit(0);
}
else
{
printf("this is a bmp file!");
}
//读取信息头
if (fread(&info_header, sizeof(BITMAPINFOHEADER), 1, bmpfile) != 1)
{
printf("read info header error!");
exit(0);
}
(4)保证每一行的数据量为4的整数倍,保证行数为偶数
//为了保证图像大小是4的倍数,所以每一行的数据量是4字节的整数倍
if (info_header.biWidth % 4 == 0)
{
width = info_header.biWidth;
}
else
{
width = (info_header.biWidth * info_header.biBitCount + 31) / 32 * (32 / 8);
//(x+31)/32,结果为下取整,得到结果以后,再乘以32,就能得到比原位数大且是32倍数的数字,再除以8得到字节数
}
//保证行数是偶数
if (info_header.biHeight % 2 == 0)
{
height = info_header.biHeight;
}
else
{
height = info_header.biHeight + 1;
}
(5)开辟缓冲区
rgbbuf = new unsigned char[height * width * 3];
ybuf = new unsigned char[height * width];
ubuf = new unsigned char[height * width / 4];
vbuf = new unsigned char[height * width / 4];
unsigned char* utmp = new unsigned char[height * width];
unsigned char* vtmp = new unsigned char[height * width];
(6)之后调用BMP2RGB函数将BMP转为RGB;
调用RGB2YUV函数将RGB转为YUV,
此时的YUV为4:4:4的取样结构,因此还需调用DownYUV进行下采样,使YUV的取样格式为4:2:0
BMP2RGB(width, height, bmpfile, file_header, info_header, rgbbuf);
RGB2YUV(width, height, rgbbuf, ybuf, utmp, vtmp);
DownYUV(width, height, utmp, vtmp, ubuf, vbuf);
(7)写入YUV
for (int i = 0; i < repeat_num; i++)
{
fwrite(ybuf, 1, width * height, yuvfile);
fwrite(ubuf, 1, width * height / 4, yuvfile);
fwrite(vbuf, 1, width * height / 4, yuvfile);
}
(8)释放缓冲区,关闭文件
if (rgbbuf)
delete[] rgbbuf;
if (ybuf)
delete[] ybuf;
if (ubuf)
delete[] ubuf;
if (vbuf)
delete[] vbuf;
if (utmp)
delete[] utmp;
if (vtmp)
delete[] vtmp;
fclose(bmpfile);
fclose(yuvfile);
3.BMP2RGB.cpp
因为BMP为倒序存放,所以读入BMP数据信息后,要进行倒序到正序的转化;
然后根据每像素位数的不同,执行不同的操作:
在24位图中直接取像素数据写RGB缓冲区,
16位图中,最低的5位表示蓝色分量,中间的6位标识绿色分量,高的5位表示红色分量,需要位与移位取像素数据转换为8bit/彩色分量,写RGB缓冲区;
8bit以下需要构造调色板,位与移位取像素数据,用掩码mask来遮蔽暂时不需要的比特位,查调色板,写RGB缓冲区。
void BMP2RGB(int width, int height, FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, unsigned char* rgbout)
{
unsigned long loop;
unsigned char mask, * index_data, * data;
int w, h;
w = width * info_header.biBitCount/8;
h = height;
//倒序前数据缓冲区
index_data = new unsigned char[h * w];
//倒序后数据缓冲区
data = new unsigned char[h * w];
fseek(bmpfile, file_header.bfOffBits, 0);
if (fread(index_data, h * w, 1, bmpfile) != 1)
{
printf("read file error!");
exit(0);
}
//倒序变正序
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
data[i * w + j] = index_data[(h - 1 - i) * w + j];
}
}
//24位,直接将倒序后的缓存区数据复制给缓存输出区
if (info_header.biBitCount == 24)
{
memcpy(rgbout, data, h * w);//保存顺序是BGR
}
//16位,位与移位取像素数据转换为8bit/彩色分量,写RGB缓冲区
if (info_header.biBitCount == 16)
{
for (loop = 0; loop < h * w; loop += 2)
{
*rgbout = (data[loop] & 0x1F) << 3;
*(rgbout + 1) = ((data[loop] & 0xE0) >> 2) + ((data[loop + 1] & 0x03) << 6);
*(rgbout + 2) = (data[loop + 1] & 0x7C) << 1;
rgbout += 3;
}
}
//8bit以下,位与移位取像素数据,查调色板,写RGB缓冲区
RGBQUAD* pRGB = (RGBQUAD*)malloc(sizeof(RGBQUAD) * (unsigned int)pow((float)2, (float)info_header.biBitCount));
if (!MakePalette(bmpfile, file_header, info_header, pRGB))
printf("no palette");
if (info_header.biBitCount <= 8)
{
for (loop = 0; loop < h * w; loop++)
{
switch (info_header.biBitCount)
{
case 1:
mask = 0x80;
break;
case 2:
mask = 0xC0;
break;
case 4:
mask = 0xF0;
break;
case 8:
mask = 0xFF;
}
int shiftcnt = 1;
while (mask)
{
unsigned char index = mask == 0xFF ? data[loop] : ((data[loop] & mask) >> (8 - shiftcnt * info_header.biBitCount));
*rgbout = pRGB[index].rgbBlue;
*(rgbout + 1) = pRGB[index].rgbGreen;
*(rgbout + 2) = pRGB[index].rgbRed;
if (info_header.biBitCount == 8)
mask = 0;
else
mask >>= info_header.biBitCount;
rgbout += 3;
shiftcnt++;
}
}
}
}
调色板MakePalette
bool MakePalette(FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, RGBQUAD* rgbout)
{
if ((file_header.bfOffBits - sizeof(BITMAPFILEHEADER) - info_header.biSize) == sizeof(RGBQUAD) * pow(2, info_header.biBitCount))
{
fseek(bmpfile, sizeof(BITMAPFILEHEADER) + info_header.biSize, 0);
fread(rgbout, sizeof(RGBQUAD), (unsigned int)pow(2, info_header.biBitCount), bmpfile);
return true;
}
else
return false;
4.RGB2YUV.cpp
采用部分查找表法,提高运行效率
void initLookupTable()
{
for (int i = 0; i < 256; i++)
{
RGBYUV02990[i] = (float)0.2990 * i;
RGBYUV05870[i] = (float)0.5870 * i;
RGBYUV01140[i] = (float)0.1140 * i;
RGBYUV01684[i] = (float)0.1684 * i;
RGBYUV03316[i] = (float)0.3316 * i;
RGBYUV04187[i] = (float)0.4187 * i;
RGBYUV00813[i] = (float)0.0813 * i;
}
}
RGB2YUV计算公式
要注意计算后的数据越界问题。超出最大范围的按照最大值处理,超出最小范围的按照最小值处理。
void adjust(unsigned char* b, unsigned char* g, unsigned char* r, unsigned char* y, unsigned char* u, unsigned char* v)
{
float temp = 0;
temp = (float)(RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
temp > 235 ? 235 : temp;
temp < 16 ? 16 : temp;
*y = (unsigned char)temp;
temp = (float)(-RGBYUV01684[*r] - RGBYUV03316[*g] +(*b)/2+128);
temp > 240 ? 240 : temp;
temp < 16 ? 16 : temp;
*u = (unsigned char)temp;
temp = (float)((*r)/2-RGBYUV04187[*g] - RGBYUV00813[*b]+ 128);
temp > 240 ? 240 : temp;
temp < 16 ? 16 : temp;
*v = (unsigned char)temp;
}
void RGB2YUV(int width, int height, unsigned char* rgbbuf, unsigned char* ybuf, unsigned char* ubuf, unsigned char* vbuf)
{
initLookupTable();
long size;
unsigned char *r, * g, * b;
unsigned char *y, * u, * v;
size = width * height;
b = rgbbuf;
y = ybuf;
u = ubuf;
v = vbuf;
for (int i = 0; i < size; i++)
{
g = b + 1;
r = b + 2;
adjust(b,g,r,y,u,v);
b += 3;
y++;
u++;
v++;
}
ybuf = y;
ubuf = u;
vbuf = v;
}
5.DownYUV.cpp
4:2:0取样结构如下,我们用小正方形内四个数据值的平均值作为一个色度数据,由此实现下采样。
代码:
void DownYUV(int width, int height, unsigned char* utmp, unsigned char* vtmp, unsigned char* ubuf, unsigned char* vbuf)
{
unsigned char* ut = utmp;
unsigned char* vt = vtmp;
unsigned char* ub = ubuf;
unsigned char* vb = vbuf;
int k = 0;
float u, v;
for (int i = 0; i < height * width / 4; i++)
{
u = (float)(*(ut + k) + *(ut + k + 1) + *(ut + width + k) + *(ut + width + k + 1))/4.0;
v = (float)(*(vt + k) + *(vt + k + 1) + *(vt + width + k) + *(vt + width + k + 1)) / 4.0;
*ub = (unsigned char)u;
*vb = (unsigned char)v;
ub++;
vb++;
if ((i + 1) % (width / 2) == 0)
{
k = k + 2 + width;
}
else
k = k + 2;
}
ubuf = ub;
vbuf = vb;
}