1、bmp图像编码
bmp即bitmap,也就是位图,一般由4部分组成:文件头信息块、图像描述信息块、颜色表(在真彩色模式无颜色表)和图像数据区。
在图像数据之前,如图所示,共有54位数据
其中,0x424d在十进制为19778,对应的ascii码为bm,表示这是个bitmap
文件。
在c语言中,short类型为16位,即2字节;int为4字节。考虑到bmp格式的文件头中,每个信息基本都是2字节的倍数,故而用int和short
便可描述出bmp格式的文件头。
文件信息头[14 bytes]存储着文件类型,文件大小等信息
// 文件信息头结构体 typedef struct tagbitmapfileheader{ unsigned short bftype; //必为'bm' unsigned int bfsize; //文件字节数(2-5) unsigned int bfreserved; //位图文件保留字,必为0(6-9) unsigned int bfoffbits; //像素数据偏移 (10-13) } bmpheader;
接下来的40bytes存储图像的尺寸,颜色索引,位平面数等信息
#define uint unsigned int #define ushort unsigned short //图像信息头结构体 typedef struct tagbitmapinfoheader{ uint bisize; // 结构体尺寸 (14-17) int biwidth; // 图像宽度 (18-21) int biheight; // 图像高度 (22-25) ushort biplanes; // 目标设备的级别,为1(26-27) ushort bibitcount; // 像素位数,为1、4、8或24(28-29) uint bicompression; // 位图压缩类型,0为不压缩、1为bi_rle8、2为bi_rle4(30-33) uint bisizeimage; // 单像素数据大小,等于bfsize-bfoffbits (34-37) int bixpelspermeter; // 水平分辨率,一般为0 (38-41) int biypelspermeter; // 垂直分辨率,一般为0 (42-45) uint biclrused; // 位图颜色表中的颜色数,0表示使用所有调色板项(46-49) uint biclrimportant; // 重要颜色索引的数目,0表示都重要(50-53) } infoheader;
在54位的文件头之后,如果需要的话,bmp文件可存放调色板信息,对于rgb图像来说,可以如下
//24位图像素信息结构体,即调色板 typedef struct _pixelinfo { unsigned char rgbblue; //蓝色分量 (0-255) unsigned char rgbgreen; //绿色分量 (0-255) unsigned char rgbred; //红色分量 (0-255) unsigned char rgbreserved;// 保留,必须为0 } pixelinfo;
颜色表中rgbquad结构数据的个数由bibitcount来确定:
- 当bibitcount=1,4,8时,分别有2,16,256个表项;
- 当bibitcount=24时,没有颜色表项。
位图信息头和颜色表组成位图信息,bitmapinfo结构定义如下:
typedef struct tagbitmapinfo { bitmapinfoheader bmiheader; // 位图信息头 rgbquad bmicolors[1]; // 颜色表 } bmpinfo;
颜色表接下来为图像数据区,存储每个像素对应的颜色号,单像素点颜色号的位长度与图像类型有关
图像类型 | 2色图像 | 16色图像 | 256色图像 | 真彩色图像 |
---|---|---|---|---|
字节数 | 1bit | 4bit | 1b(8bit) | 3b |
由此可知,理想情况下图像数据区的字节数为:
但是,由于bmp以字节为单位,按行存储图片的数据,也就是说每一行必须为字节数。而对于16色以及2色图像而言,当每行像素个数为奇数时,则必然导致出现空位,从而实际所占存储空间要大于上述计算值。
2、读取bmp文件
读取文件之后第一件事即判断我们读取的是否为bmp图像,即
//判断是否是位图,在0-1字节 int isbitmap(file *fp){ ushort s; fread(&s,1,2,fp); return s==bm ? 1 : 0; }
其中,fread命令表示读取文件fp,并将读取到的内容存储在s中。1表示要读取的数据的大小,2表示将要读取的元素的个数。由前文可知,位图的文件类型必须是bm,十六进制下0x4d42,十进制为19778。
然后需要得到图像的宽度和高度,从而确定数据区的范围
//获得图片的宽度,在18-21字节 int getwidth(file *fp){ int width; fseek(fp,18,seek_set); fread(&width,1,4,fp); return width; } //获得图片的高度 ,在22-25字节 int getheight(file *fp){ int height; fseek(fp,22,seek_set); fread(&height,1,4,fp); return height; }
其中,fseek
可以为fp提供一个偏移量,seek_set表示从文件的开头进行移动。由上文可知,图片高度所在位置是第22个字节。
若想读取其他信息,以此类推即可。
//test.c #include<stdio.h> #include<malloc.h> #define bm 19778 // 位图的标志 #define path "1.bmp" //打开的文件路径 #define ushort unsigned short #define uint unsigned int #define uchar unsigned char //判断是否是位图,在0-1字节 int isbitmap(file *fp){ ushort s; fread(&s,1,2,fp); return s==bm ? 1 : 0; } //获得图片的宽度,在18-21字节 int getwidth(file *fp){ int width; fseek(fp,18,seek_set); fread(&width,1,4,fp); return width; } //获得图片的高度 ,在22-25字节 int getheight(file *fp){ int height; fseek(fp,22,seek_set); fread(&height,1,4,fp); return height; } //获得每个像素的位数,在28-29字节 ushort getbit(file *fp) { ushort bit; fseek(fp,28,seek_set); fread(&bit,1,2,fp); return bit; } //获得数据的起始位置,在10-13字节 uint getoffset(file *fp){ uint offset; fseek(fp,10l,seek_set); fread(&offset,1,4,fp); return offset; } int main(){ int width,height; file *fp=fopen(path,"r"); uchar *r,*g,*b; int i,j; r=(uchar *)malloc(4000); b=(uchar *)malloc(4000); g=(uchar *)malloc(4000); if(!isbitmap(fp)){ printf("format error!n"); fclose(fp); return 0; } printf("this file is a bitmap picturen"); printf("width = %ldnheight = %ldn",getwidth(fp),getheight(fp)); printf("bit size = %d bitn",getbit(fp)); printf("offset = %dn",getoffset(fp)); return 0; }
验证:
> gcc .test.c
> .a.exe
this file is a bitmap picture
width = 3840
height = 2160
bit size = 24 bit
offset = 138
到此这篇关于c语言如何读取bmp图像的文章就介绍到这了,更多相关c读取bmp图像内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
需要了解更多c/c++开发分享C语言如何读取bmp图像,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/1072587.html