转自: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);
}

[转]RGB数据保存为BMP图片的更多相关文章

  1. 纯C++代码实现将像素矩阵保存为bmp图片

    由于工作需要,时常需要将像素矩阵保存图片显示观看.为此,特地总结了三种使用纯C++代码生成bmp图片的方法.分别是使用自定义数据.从外界导入的txt和csv以及从图片中导入的数据. 1.使用自定义数据 ...

  2. 【VC++技术杂谈006】截取电脑桌面并将其保存为bmp图片

    本文主要介绍如何截取电脑桌面并将其保存为bmp图片. 1. Bmp图像文件组成 Bmp是Windows操作系统中的标准图像文件格式. Bmp图像文件由四部分组成: (1)位图头文件数据结构,包含Bmp ...

  3. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...

  4. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  5. [转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  6. javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  7. Windows 保存BMP图片

    在Windows下保存BMP图片还是挺方便的,直接上代码,拷贝就能用 void savebmp(uchar * pdata, char * bmp_file, int width, int heigh ...

  8. BCB实现BMP图片的RGB分解(转)

    源:BCB实现BMP图片的RGB分解 1.打开BMP图片文件,在Image控件中显示: if(dlgOpen1->Execute()) { edt1->Text=dlgOpen1-> ...

  9. bmp图片数据提取

    仿照别人的程序写的bmp数据提取C代码,并将提取的数据放到txt文档中 /* date : 2014/06/24 designer :pengxiaoen version : dev4.9.9.0 f ...

随机推荐

  1. HTML5游戏开发系列教程8(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-8/ 这是我们最新一篇HTML5游戏开发系列文章.我们将继续使用c ...

  2. php微信支付接口开发程序(流程已通)

    php微信支付接口开发程序(流程已通) 来源:未知    时间:2014-12-11 17:11   阅读数:11843   作者:xxadmin [导读] 微信支付接口现在也慢慢的像支付宝一个可以利 ...

  3. 2017 Multi-University Training Contest - Team 4 hdu6069 Counting Divisors

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6069 题目: Counting Divisors Time Limit: 10000/5000 ...

  4. python os模块一些常用操作

    os.getcwd() ## 获取当前路径 os.chdir("dirpath") ## 改变目录 os.makedirs("dirname") ## 递归创建 ...

  5. armv7 armv7s arm64 i386 x86_64

    开发SDK的同学需要了解这些指令集代表什么. armv7|armv7s|arm64都是ARM处理器的指令集 i386|x86_64 是Mac处理器的指令集 arm64:iPhone7 | iphone ...

  6. win7_32下编译FFmpeg

    运行环境:  VC2010软件:                 [附:本文所用软件安装包:http://download.csdn.NET/detail/sinat_36666600/9705438 ...

  7. Linux CentOS6环境下MySQL5.1升级至MySQL5.5版本过程

    转载地址:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环境,其 ...

  8. Python爬虫学习笔记之爬虫基础库

    知识预览 beautifulsoup的简单使用 beautifulsoup的遍历文档树 beautifulsoup的搜索文档树 beautifulsoup的css选择器 回到顶部 beautifuls ...

  9. oracle中的异常处理方法

    异常处理create or replace procedure prc_get_sex (stuname student.name%type) as stusex student.sex%type; ...

  10. 在Windows下安装运行Kafka

    一.安装JAVA JDK 1.下载安装包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...