转自:http://blog.csdn.net/yixianfeng41/article/details/52591585

一、BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。

1、BMP文件头(14字节)
typedef struct                       /**** BMP file header structure ****/
{
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} MyBITMAPFILEHEADER;
2、位图信息头(40字节)
typedef struct                       /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} MyBITMAPINFOHEADER;
3、颜色表

颜色表用于说明位图中的颜色,它有若干个表项,每一个表项是一个RGBQUAD类型的结构,定义一种颜色。RGBQUAD结构的定义如下:

typedef struct tagRGBQUAD{
BYTE rgbBlue;//蓝色的亮度(值范围为0-255)
BYTE rgbGreen;//绿色的亮度(值范围为0-255)
BYTE rgbRed;//红色的亮度(值范围为0-255)
BYTE rgbReserved;//保留,必须为0
}RGBQUAD;

颜色表中的RGBQUAD结构数据的个数由biBitCount来确定:当biBitCount=1,4,8时,分别为2,16,256个表项;当biBitCount=24时,没有颜色表项。

4、位图数据

位图数据记录了位图的每一个像素值,记录顺序是在扫描行内是从左到右,扫描行之间是从下到上。位图的一个像素值所占的字节数:

当biBitCount=1时,8个像素占1个字节;

当biBitCount=4时,2个像素占1个字节;

当biBitCount=8时,1个像素占1个字节;

当biBitCount=24时,1个像素占3个字节,按顺序分别为B,G,R;

二、将rgb数据保存为bmp图片的方法

void CDecVideoFilter::MySaveBmp(const char *filename,unsigned char *rgbbuf,int width,int height)
{
MyBITMAPFILEHEADER bfh;
MyBITMAPINFOHEADER bih;
/* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
unsigned short bfType=0x4d42;
bfh.bfReserved1 = ;
bfh.bfReserved2 = ;
bfh.bfSize = +sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+width*height*;
bfh.bfOffBits = 0x36; bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = ;
bih.biBitCount = ;
bih.biCompression = ;
bih.biSizeImage = ;
bih.biXPelsPerMeter = ;
bih.biYPelsPerMeter = ;
bih.biClrUsed = ;
bih.biClrImportant = ; FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Could not write file\n");
return;
} /*Write headers*/
fwrite(&bfType,sizeof(bfType),,file);
fwrite(&bfh,sizeof(bfh),, file);
fwrite(&bih,sizeof(bih),, file); fwrite(rgbbuf,width*height*,,file);
fclose(file);
}

最新文章

  1. RabbitMQ 基础概念
  2. Python成长笔记 - 基础篇 (十)
  3. Win10 UWP应用发布流程
  4. MDX Order排序
  5. 【python】python文件和目录操作方法大全(含实例)
  6. jar 命令打war包
  7. C#拓展练习之模拟键盘录入
  8. 远程连接sql server 数据库的方法
  9. tyvj1185营业额统计
  10. Retrieving the COM class factory for component with CLSID XX failed due to the following error: 80070005 拒绝访问。
  11. 转载: Nova-Router 分析
  12. 读取txt内文件内容
  13. 设计模式之代理模式(Proxy)(2)
  14. A1021. Deepest Root
  15. 在android模拟器上http 链接的图片地址可能不会显示
  16. robotium之does not have a signature matching问题
  17. DapperExtensions and Dapper.Contrib在表构架不是默认dbo时的处理 DapperExtensions and Dapper.Contrib with non-dbo Schema
  18. ServiceWorker和WebWorker
  19. Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
  20. 现代cpu的合并写技术对程序的影响

热门文章

  1. HMM、MEMM、CRF模型比较和标注偏置问题(Label Bias Problem)
  2. Android Study Notes
  3. boost单元测试框架
  4. java通过url抓取网页数据-----正则表达式
  5. co.js异步回调原理理解
  6. 网页采集利器 phpQuery
  7. Centos 6.5 Install Mysql 8.0.0
  8. httpfs的使用
  9. 带你走进ajax(2)
  10. 【DeepLearning学习笔记】Neurons神经元