BMP文件读取

读取BMP文件内部信息和位置像素点信息

 详细代码如下,(有需要可以查看知识点分布。如有错误,还望指针。谢谢。)

#include 
#include 
#include 
using namespace std;
typedef unsigned short U16;
typedef unsigned long  U32;
#pragma pack(1) //设置1字节对齐模式,pack()将对齐模式取消
/*位图文件头*/
typedef struct BMP_FILE_HEADER
{
	U16 bType;             /*  文件标识符          */
	U32 bfSize;            /*  文件的大小,单位字节 */
	U16 bReserved1;        /*  保留值,必须设置为0  */       
	U16 bReserved2;        /*  保留值,必须设置为0  */
	U32 bOffset;           /*  文件头的最后到图像数据位开始的偏移量    */
} BMPFILEHEADER;
#pragma pack()
/*位图信息头*/
typedef struct BMP_INFO
{
	U32 bInfoSize;        /*  信息头的大小             */
	U32 bWidth;           /*  图像的宽度               */
	U32 bHeight;          /*  图像的高度               */
	U16 bPlanes;          /*  图像的位面数             */
	U16 bBitCount;        /*  每个像素的位数           */
	U32 bCompression;     /*  压缩类型                 */
	U32 bmpImageSize;     /*  图像的大小,以字节为单位   */
	U32 bXPelsPerMeter;   /*  水平分辨率               */
	U32 bYPelsPerMeter;   /*  垂直分辨率               */
	U32 bClrUsed;         /*  使用的色彩数             */
	U32 bClrImportant;    /*  重要的颜色数             */
} BMPINF;
//#pragma pack()

/*彩色表*/
typedef struct RGB_QUAD 
{
	U16 rgbBlue;         /*  蓝色强度  */
	U16 rgbGreen;        /*  绿色强度  */
	U16 rgbRed;          /*  红色强度  */
	U16 rgbReversed;     /*  保留值    */
} RGBQUAD;
//读取头文件信息
string ReadBMPFileHeader( BMPFILEHEADER &bmpFileHead)
{
	string temp = "";
	string fileHeader = "";
	stringstream strStream;

	fileHeader += "位图文件头>>>\n文件标识符:";
	strStream << hex << bmpFileHead.bType;
	strStream >> temp;
	fileHeader += temp;
	temp = "";
	strStream.str("");strStream.clear();

	fileHeader += "\n文件大小:";
	strStream << dec << bmpFileHead.bfSize;
	strStream >> temp;
	fileHeader += temp;
	temp = "";
	strStream.str("");strStream.clear();

	fileHeader += "\n位图文件保留字1:";
	strStream << dec << bmpFileHead.bReserved1;
	strStream >> temp;
	fileHeader += temp;
	temp = "";
	strStream.str("");strStream.clear();

	fileHeader += "\n位图文件保留字2:";
	strStream << dec << bmpFileHead.bReserved2;
	strStream >> temp;
	fileHeader += temp;
	temp = "";
	strStream.str("");strStream.clear();

	fileHeader += "\n文件头偏移字节数:";
	strStream << dec << bmpFileHead.bOffset;
	strStream >> temp;
	fileHeader += temp;
	temp = "";
	strStream.str("");strStream.clear();

	return fileHeader;
}
//读取位图信息
string ReadBMPINF(BMPINF &bmpInfo)
{
	string temp = "";
	string strBMPInfo = "";
	stringstream strStream;
	strBMPInfo += "\n\n位图信息头>>>>\n信息头的大小:";
	strStream << dec << bmpInfo.bInfoSize;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n图像的宽度:";
	strStream << dec << bmpInfo.bWidth;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n图像的高度:";
	strStream << dec << bmpInfo.bHeight;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n图像的位面数:";
	strStream << dec << bmpInfo.bPlanes;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n每个像素的位数:";
	strStream << dec << bmpInfo.bBitCount;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n压缩类型:";
	strStream << dec << bmpInfo.bCompression;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n图像的大小,以字节为单位:";
	strStream << dec << bmpInfo.bmpImageSize;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n水平分辨率:";
	strStream << dec << bmpInfo.bXPelsPerMeter;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n垂直分辨率:";
	strStream << dec << bmpInfo.bYPelsPerMeter;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n使用的色彩数:";
	strStream << dec << bmpInfo.bClrUsed;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	strBMPInfo += "\n重要的颜色数:";
	strStream << dec << bmpInfo.bClrImportant;
	strStream >> temp;
	strBMPInfo += temp;
	temp = "";
	strStream.str("");strStream.clear();

	return strBMPInfo;
}
bool ReadBMP(char* strFileName)
{
	FILE* fp;
	BMPFILEHEADER fileHeader = {0};
	BMPINF infoHeader = {0};
	long offset, bmpImageSize, width, height, bytesPerPixel, size, bitCount;
	int i = 0, j = 0, count = 0;
	int lcount = 0;
	int cl[3] = {0};
	U16 c;
	fp = fopen(strFileName, "rb");
	if(fp == NULL){
		cout << "文件打开失败" << endl;
		return false;
	}
	cout << "FileName:" << strFileName << endl;

	fread(&fileHeader, sizeof(BMPFILEHEADER), 1, fp);
	fread(&infoHeader, sizeof(BMPINF), 1, fp);
	cout << ReadBMPFileHeader(fileHeader);
	cout << ReadBMPINF(infoHeader);
	cout << endl;

	unsigned char *bmpData = new unsigned char[infoHeader.bHeight*infoHeader.bWidth*3];
	fseek(fp, fileHeader.bOffset, SEEK_SET);
	fread(bmpData, 1, infoHeader.bHeight*infoHeader.bWidth*3, fp);

	for(int i = 0; i < infoHeader.bHeight; ++i){
		for(int j = 0; j < infoHeader.bWidth; ++j){
			printf("%d ", bmpData[i*infoHeader.bWidth*3+j*3]);
			printf("%d ", bmpData[i*infoHeader.bWidth*3+j*3+1]);
			printf("%d ", bmpData[i*infoHeader.bWidth*3+j*3+2]);
		}
	}
	fclose(fp);
	return true;
}
int main(void)
{
	ReadBMP("1.bmp");
	return 0;
}


用到的知识点:

1.位图基本信息存储结构

2.读取位图中像素点信息

3.其他的知识

 

位图基本信息存储结构

#pragma pack(1) //设置1字节对齐模式,pack()将对齐模式取消

/*位图文件头*/

typedef struct BMP_FILE_HEADER

{

         U16bType;             /*  文件标识符          */

         U32bfSize;            /*  文件的大小,单位字节 */

         U16bReserved1;        /*  保留值,必须设置为0  */      

         U16bReserved2;        /*  保留值,必须设置为0  */

         U32bOffset;           /*  文件头的最后到图像数据位开始的偏移量    */

} BMPFILEHEADER;

#pragma pack()

/*位图信息头*/

typedef struct BMP_INFO

{

         U32bInfoSize;        /*  信息头的大小             */

         U32bWidth;           /*  图像的宽度               */

         U32bHeight;          /*  图像的高度               */

         U16bPlanes;          /*  图像的位面数             */

         U16bBitCount;        /*  每个像素的位数           */

         U32bCompression;     /*  压缩类型                 */

         U32bmpImageSize;     /*  图像的大小,以字节为单位   */

         U32bXPelsPerMeter;   /*  水平分辨率               */

         U32bYPelsPerMeter;   /*  垂直分辨率               */

         U32bClrUsed;         /*  使用的色彩数             */

         U32bClrImportant;    /*  重要的颜色数             */

} BMPINF;

//#pragma pack()

 

/*彩色表*/

typedef struct RGB_QUAD

{

         U16rgbBlue;         /*  蓝色强度  */

         U16rgbGreen;        /*  绿色强度  */

         U16rgbRed;          /*  红色强度  */

         U16rgbReversed;     /*  保留值    */

} RGBQUAD;

/

读取位图中像素点信息

在读取像素点阶段中,彩色图片中包含了RGB三色。width’+height’表示那个像素点的位置。具体代码:

for(int i = 0; i < infoHeader.bHeight;++i){

         for(intj = 0; j < infoHeader.bWidth; ++j){

                   printf("%d", bmpData[i*infoHeader.bWidth*3+j*3]);

                   printf("%d", bmpData[i*infoHeader.bWidth*3+j*3+1]);

                   printf("%d", bmpData[i*infoHeader.bWidth*3+j*3+2]);

         }

}

/

其他的知识

stringstream ss;需要包含的头文件#include using namespace std;

#pragma pack(num)表示变量所占字节位数,当num= 0时取消占位数


你可能感兴趣的:(图像)