您的位置:首页 > 编程语言 > C语言/C++

c语言读取bmp文件

2017-12-01 16:12 281 查看
一、了解BMP文件的格式:位图文件头、位图信息段、调色板信息(一般都没有)、位图数据,各部分关键字段。

详细信息参见:http://blog.csdn.net/lanbing510/article/details/8176231

读取BMP文件的函数所需要的头文件:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <malloc.h>

#include "bmpstru.h"

#define Malloc(type, n) (type *)malloc(n * sizeof(type))

二中的结构体定义我建立了一个头文件 "bmpstru.h",并且将该函数封装了,以后使用直接调用。

二、bmpstru.h中构造位图文件头、位图信息段、位图数据结构体

1)位图文件头                                              

typedef struct BmpHeader{    

char bfType[10];              
int bfSize;
int bfReserved1;
int bfReserved2;
int bfoffBits;

} BMPHEADER;

2)位图信息段

typedef struct BmpInfoHeader{
int biSize;
int biWidth;
int biHeight;
int bitPlanes;
int biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;

} BMPINFOHEADER;

3)位图数据

typedef struct BmpMata{
unsigned int r;
unsigned int g;
unsigned int b;

}BMPDATA;

三、读取BMP文件函数用fopen()打开文件,并判断是否打开成功

if((file = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "文件打开失败!");
exit(1);
}

四、读取BMP文件函数初始化位图文件头、位图信息段

//读取bmp的BMPHEADER信息

memset(tmp, 0, 20);
fread(tmp, 1, 1, file);
fread(&tmp[1], 1, 1, file);

  //pic.bmphd.bfType
//itoa(tmp[0], &pic.bmphd.bfType[0], 10);
//itoa(tmp[1], &pic.bmphd.bfType[1], 10);

//printf("success...");
//system("pause");
memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmphd.bfSize = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 2, file);
pic.bmphd.bfReserved1 = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 2, file);
pic.bmphd.bfReserved2 = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmphd.bfoffBits = tmp[0];

//读取bmp的BMPINFOHEADER信息
memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biSize = tmp[0];

//图像width
memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biWidth = tmp[0];

//图像height
memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biHeight = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1,2, file);
pic.bmpinfohd.bitPlanes = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 2, file);
pic.bmpinfohd.biBitCount = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biCompression = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biSizeImage = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biXPelsPerMeter = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biYPelsPerMeter = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biClrUsed = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 4, file);
pic.bmpinfohd.biClrImportant = tmp[0];

获得了图片的高和宽:pic.bmpinfohd.biWidth、
pic.bmpinfohd.biHeight;


五、构造二维数组,并读取RGB值

BMPDATA **pixel = Malloc(BMPDATA *, pic.bmpinfohd.biHeight);
for(int i = 0; i < pic.bmpinfohd.biHeight; i++)
{
pixel[i] = Malloc(BMPDATA, pic.bmpinfohd.biWidth);
}

for(int i = 0; i < pic.bmpinfohd.biHeight; i++)
for(int j = 0; j < pic.bmpinfohd.biWidth; j++)
{
memset(tmp, 0, 20);
fread(tmp, 1, 1, file);
pixel[i][j].r = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 1, file);
pixel[i][j].g = tmp[0];

memset(tmp, 0, 20);
fread(tmp, 1, 1, file);
pixel[i][j].b = tmp[0];
}

经过上述5步,BMP文件的RGB值就保存到pixel二维数组中了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 bmp 读取