1、bmp格式介绍
bmp格式图片里实际存储的也是RGB原始数据,可以分为8bit、16bit、24bit、32bit的bmp格式,也就是指bmp图片中保存的RGB是用8bit、16bit、24bit、32bit来表示。简单理解就是在原始RGB的数据前面加bmp文件头,文件头里会说明保存的RGB的位数、宽高、文件大小等信息。更多bmp格式的细节参考博客:https://blog.csdn.net/aidem_brown/article/details/80500637
2、bmp格式的注意点
(1)保存的RGB原始数据,排列方式是BGR、BGR······格式; (2)保存的RGB图像是倒立的,是从下往上一行一行保存的; (3)RGB数据的一行长度要4字节对齐。比如:宽是102,那每行数据是102*3=306,这时候要向上对齐,每行是308,对的两个字节用0填充。
3、代码思路
(1)把RGB图像转换成BGR、BGR排列顺序; (2)把BGR数据旋转180度,需要得到倒立的BGR图像; (2)根据BGR图像的宽、高等信息构建bmp文件头; (3)将bmp文件头、BGR数据依次写入到xxx.bmp文件中,得到bmp格式的图像;
4、实现代码
#define align(value, align) ((( (value) + ( (align) - 1 ) ) \
/ (align) ) * (align) )
typedef struct
{
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BITMAPFILEHEADER;
typedef struct
{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BITMAPINFOHEADER;
int BGR888_TO_BMP(char *dst, int dstLength, unsigned char *bgrbuf,int width,int height)
{
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
int i = 0, row_align;
if(dstLength < width * height * 3 + 54)
{
printf("dst buffer is too small!\n");
return OSA_EFAIL;
}
row_align = align(width * 3, 4);
unsigned short bfType=0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + row_align * height;
bfh.bfOffBits = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
memset(dst, 0, dstLength);
memcpy(dst, &bfType, sizeof(bfType));
memcpy(dst + sizeof(bfType), &bfh, sizeof(bfh));
memcpy(dst + sizeof(bfType) + sizeof(bfh), &bih, sizeof(bih));
for(i = 0; i < height; i++)
{
memcpy(dst + sizeof(bfType) + sizeof(bfh) + sizeof(bih) + row_align * i, bgrbuf + width * 3 * i, width * 3);
}
return 0;
}
|