1.BMP文件格式
BMP文件格式组成部分:bmp文件头(14个字节) + 位图信息头(40个字节) + 调色板(由颜色索引数决定) + 位图数据(由图像尺寸决定)
位图文件头BITMAPFILEHEADER |
位图信息头BITMAPINFOHEADER |
调色板Palette(可选) |
实际的位图数据ImageDate |
第一部分为位图文件头BITMAPFILEHEADER,是一个结构,其定义如下:
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
这个结构的长度是固定的,为14个字节(WORD为无符号16位整数,DWORD为无符号32位整数),各个域的说明如下:
bfType |
指定文件类型,必须是0x424D,即字符串“BM”,也就是说所有.bmp文件的头两个字节都是“BM”。 |
bfSize |
指定文件大小,包括这14个字节。 |
bfReserved1 |
保留字,不用考虑 |
bfReserved2 |
保留字,不用考虑 |
bfOffBits |
为从文件头到实际的位图数据的偏移字节数,前三个部分的长度之和。 |
第二部分为位图信息头BITMAPINFOHEADER,也是一个结构,其定义如下:
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
这个结构的长度是固定的,为40个字节(LONG为32位整数),各个域的说明如下:
biSize |
指定这个结构的长度,为40。 |
biWidth |
指定图象的宽度,单位是像素。 |
biHeight |
指定图象的高度,单位是像素。 |
biPlanes |
必须是1,不用考虑。 |
biBitCount |
指定表示颜色时要用到的位数,常用的值为1(黑白二色图), 4(16色图), 8(256色), 24(真彩色图), 32(带透明通道的真彩色图) |
biCompression |
指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS(都是一些Windows定义好的常量)。一般只使用第一种不压缩的情况,即biCompression为BI_RGB的情况。 |
biSizeImage |
指定实际的位图数据占用的字节数,其实也可以从以下的公式中计算出来: biSizeImage=biWidth’ × biHeight 要注意的是:上述公式中的biWidth’必须是4的整倍数(所以不是biWidth,而是biWidth’,表示大于或等于biWidth的,最接近4的整倍数。举个例子,如果biWidth=240,则biWidth’=240;如果biWidth=241,biWidth’=244)。 如果biCompression为BI_RGB,则该项可能为零 |
biXPelsPerMeter |
指定目标设备的水平分辨率,单位是每米的像素个数。 |
biYPelsPerMeter |
指定目标设备的垂直分辨率,单位同上。 |
biClrUsed |
指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次方。 |
biClrImportant |
指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。 |
第三部分为调色板Palette,当然,这里是对那些需要调色板的位图文件而言的。有些位图,如真彩色图,前面已经讲过,是不需要调色板的,BITMAPINFOHEADER后直接是位图数据。
调色板实际上是一个数组,共有biClrUsed个元素(如果该值为零,则有 2的biBitCount次方 个元素)。数组中每个元素的类型是一个RGBQUAD结构,占4个字节,其定义如下:
typedef struct tagRGBQUAD {
BYTE rgbBlue; //该颜色的蓝色分量
BYTE rgbGreen; //该颜色的绿色分量
BYTE rgbRed; //该颜色的红色分量
BYTE rgbReserved; //保留值
} RGBQUAD;
第四部分就是实际的图像数据了。对于用到调色板的位图,图像数据就是该像素颜在调色板中的索引值。对于真彩色图,图象数据就是实际的R、G、B值。
要注意两点:
(1) 每一行的字节数必须是4的整倍数,如果不是,则需要补齐。这在前面介绍biSizeImage时已经提到了。
(2) 一般来说,.bMP文件的数据从下到上,从左到右的。也就是说,从文件中最先读到的是图像最下面一行的左边第一个像素,然后是左边第二个像素……接下来是倒数第二行左边第一个像素,左边第二个像素……依次类推 ,最后得到的是最上面一行的最右一个象素。
2.C++读写BMP代码
2.1 头文件 BmpUtil.h
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#ifndef WINAPI
#define WINAPI __stdcall
#endif
#else
#define WINAPI
#endif
/************************************************************************
功 能:读取BMP文件
参 数:szFileName - [in]文件名
pRawBuf - [out]图像数据
iWidth - [out]图像宽度
iHeight - [out]图像宽度
iChannels - [out]图像通道数
返回值:0 - 成功,其他- 失败
************************************************************************/
int WINAPI ReadBMP(char* szFileName, unsigned char* pRawBuf, int* iWidth, int* iHeight, int *iChannels);
/************************************************************************
功 能:保存BMP文件
参 数:szFileName - [in]文件名
pRawBuf - [in]图像数据
iWidth - [in]图像宽度
iHeight - [in]图像宽度
iChannels - [in]图像通道数
返回值:0 - 成功,其他- 失败
************************************************************************/
int WINAPI SaveBMP(char* szFileName, unsigned char* pRawBuf, int iWidth, int iHeight, int iChannels);
2.2 源文件 BmpUtil.cpp
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include "BmpUtil.h"
//设置内存1字节对齐
#pragma pack (1)
//文件信息头 14个字节
typedef struct
{
unsigned short bfType; //文件的类型,该值必需是0x4D42,也就是字符'BM'
unsigned int bfSize; //该位图文件的大小,用字节为单位
unsigned short bfReserved1; //保留,必须设置为0
unsigned short bfReserved2; //保留,必须设置为0
unsigned int bfOffBits; //从文件头开始到实际的图象数据之间的字节的偏移量
} BMPBitMapFileHeader;
//位图信息头 40个字节
typedef struct
{
unsigned int biSize; //BITMAPINFOHEADER结构所需要的字数。
int biWidth; //图象的宽度,以象素为单位
int biHeight; //图象的高度,以象素为单位
unsigned short biPlanes; //目标设备的位面数,其值总是被设为1
unsigned short biBitCount; //比特数/象素,其值为1、4、8、16、24、或32
unsigned int biCompression; //图象数据压缩的类型,同样我们只讨论没有压缩的类型:BI_RGB
unsigned int biSizeImage; //图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0
int biXPelsPerMeter; //水平分辨率,用象素/米表示
int biYPelsPerMeter; //垂直分辨率,用象素/米表示
unsigned int biClrUsed; //位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项)。
unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目,如果是0,表示都重要
} BMPBitMapInfoHeader;
typedef struct
{
unsigned char rgbBlue; //该颜色的蓝色分量
unsigned char rgbGreen; //该颜色的绿色分量
unsigned char rgbRed; //该颜色的红色分量
unsigned char rgbReserved; //保留值
} BMPRgbQuad;
/*******************************************************************************************
功 能: BMP文件数据转换为内存数据
参 数: pBMPBuf - [in]BMP文件数据
iBMPLen - [in]BMP文件数据长度
pRawBuf - [out]图像裸数据
iWidth - [out]图像宽度
iHeight - [out]图像高度
iChannels - [out]图像通道数
返 回: 0-成功,其他-失败
*******************************************************************************************/
int WINAPI BMPToRaw(unsigned char* pBMPBuf, int iBMPLen,
unsigned char* pRawBuf, int* iWidth, int* iHeight, int *iChannels)
{
int X, Y,iNewWidth,channel;
BMPBitMapFileHeader bmpFileHeader;
BMPBitMapInfoHeader bmpInfoHeader;
memcpy(&bmpFileHeader,pBMPBuf, sizeof(BMPBitMapFileHeader));
memcpy(&bmpInfoHeader,pBMPBuf+sizeof(BMPBitMapFileHeader), sizeof(BMPBitMapInfoHeader));
if (bmpFileHeader.bfType != 0x4D42)
{
return -1;
}
X = bmpInfoHeader.biWidth;
Y = bmpInfoHeader.biHeight;
channel = bmpInfoHeader.biBitCount / 8;
*iWidth = X;
*iHeight = Y;
*iChannels = channel;
if (channel == 4)
*iChannels = 3;
if (pRawBuf == NULL)
{
return 0;
}
iNewWidth = (channel*X + 3) / 4 * 4;
if (channel == 1)
{
for (int i = 0; i < Y; i++)
{
memcpy(pRawBuf + i*X, pBMPBuf + 1078 + (Y - 1 - i)*iNewWidth, X*sizeof(char));
}
}
else if (channel == 3)
{
for (int i = 0; i < Y; i++)
{
memcpy(pRawBuf + i * 3 * X, pBMPBuf + 54 + (Y - 1 - i)*iNewWidth, 3 * X*sizeof(char));
}
}
else if(channel == 4)
{
for (int i = 0; i < Y; i++)
{
for (int j = 0; j < X; j++)
{
pRawBuf[i * 3 * X + 3 * j] = pBMPBuf[54 + (Y - 1 - i) * iNewWidth + 4 * j];
pRawBuf[i * 3 * X + 3 * j + 1] = pBMPBuf[54 + (Y - 1 - i) * iNewWidth + 4 * j + 1];
pRawBuf[i * 3 * X + 3 * j + 2] = pBMPBuf[54 + (Y - 1 - i) * iNewWidth + 4 * j + 2];
}
}
}
else
{
return -2;
}
return 0;
}
/*******************************************************************************************
功 能: 图像裸数据转换为BMP文件数据转
参 数: pRawBuf - [in]图像裸数据
iWidth - [in]图像宽度
iHeight - [in]图像高度
iChannels - [in]图像通道数
pBMPBuf - [out]BMP文件数据
iBMPLen - [out]BMP文件数据长度
返 回: 0-成功,其他-失败
*******************************************************************************************/
int WINAPI RawToBMP(unsigned char* pRawBuf, int iWidth, int iHeight,int iChannels,
unsigned char* pBMPBuf, int* iBMPLen)
{
BMPBitMapFileHeader bmpFileHeader;
BMPBitMapInfoHeader bmpInfoHeader;
int iNewWidth,iFileSize;
int iHeadLen = 54;
iNewWidth = (iChannels * iWidth + 3) / 4 * 4;
if (iChannels == 1)
{
iHeadLen = 1078;
}
iFileSize = iHeadLen + iHeight*iNewWidth;
bmpFileHeader.bfType = 0x4D42;
bmpFileHeader.bfSize = iFileSize;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = iHeadLen;
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = iWidth;
bmpInfoHeader.biHeight = iHeight;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = (iChannels*8);
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = iHeight*iNewWidth;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
memset(pBMPBuf, 0x00, iFileSize);
//写入文件头
memcpy(pBMPBuf, &bmpFileHeader, sizeof(BMPBitMapFileHeader));
memcpy(pBMPBuf + sizeof(BMPBitMapFileHeader), &bmpInfoHeader, sizeof(BMPBitMapInfoHeader));
if (iChannels == 1)
{
//写入调色板信息
BMPRgbQuad quard[256] = {0};
for (int j = 0;j<256; j++)
{
quard[j].rgbRed = j;
quard[j].rgbGreen = j;
quard[j].rgbBlue = j;
quard[j].rgbReserved = 0;
}
memcpy(pBMPBuf + 54, quard, 256*sizeof(BMPRgbQuad));
}
//写入图象数据
for (int i = 0; i<iHeight; i++)
{
memcpy(pBMPBuf + iHeadLen * sizeof(char) + (iHeight - 1 - i)*iNewWidth,
pRawBuf + i * iChannels * iWidth, iChannels * iWidth*sizeof(char));
}
//输出文件大小
*iBMPLen = iFileSize;
return 0;
}
/************************************************************************
功 能:以wb方式写文件
参 数:szFileName - [in]文件名
buf - [in]数据缓存
length - [in]数据大小(字节数)
返回值:0 成功 0 失败
************************************************************************/
int WINAPI SaveData(char *szFileName, unsigned char *iBuf, unsigned int iLen)
{
FILE *fp;
fp = fopen(szFileName, "wb");
if (!fp)
{
return -1;
}
if (fwrite(iBuf, 1, iLen, fp) != iLen)
{
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
/************************************************************************
功 能:获取文件大小
参 数:szFileName - [in]文件名
返回值:文件大小(字节数)
************************************************************************/
int WINAPI GetDataSize(char *szFileName)
{
FILE *fp;
fp = fopen(szFileName, ("rb"));
if (!fp)
{
return 0;
}
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fclose(fp);
return size;
}
/************************************************************************
功 能:以rb方式读文件
参 数:szFileName - [in]文件名
buf - [out]数据缓存
length - [out]数据大小(字节数)
返回值:0 成功 0 失败
************************************************************************/
int WINAPI ReadData(char *szFileName, unsigned char *oBuf, int iLen)
{
FILE *fp;
fp = fopen(szFileName, "rb");
if (!fp)
{
return -1;
}
fread(oBuf, 1, iLen, fp);
fclose(fp);
return 0;
}
/************************************************************************
功 能:读取BMP文件
参 数:szFileName - [in]文件名
pRawBuf - [out]图像数据
iWidth - [out]图像宽度
iHeight - [out]图像宽度
iChannels - [out]图像通道数
返回值:0 - 成功,其他- 失败
************************************************************************/
int WINAPI ReadBMP(char* szFileName, unsigned char* pRawBuf, int* iWidth, int* iHeight, int *iChannels)
{
int iBMPLen = GetDataSize(szFileName);
if (iBMPLen <= 0)
return -100;
unsigned char* pBMPBuf = (unsigned char*)malloc(iBMPLen);
ReadData(szFileName, pBMPBuf, iBMPLen);
int nRet = BMPToRaw(pBMPBuf, iBMPLen, pRawBuf, iWidth, iHeight, iChannels);
free(pBMPBuf);
return nRet;
}
/************************************************************************
功 能:保存BMP文件
参 数:szFileName - [in]文件名
pRawBuf - [in]图像数据
iWidth - [in]图像宽度
iHeight - [in]图像宽度
iChannels - [in]图像通道数
返回值:0 - 成功,其他- 失败
************************************************************************/
int WINAPI SaveBMP(char* szFileName, unsigned char* pRawBuf, int iWidth, int iHeight, int iChannels)
{
int iNewWidth = (iChannels * iWidth + 3) / 4 * 4;
unsigned char* pBMPBuf = (unsigned char*)malloc(iNewWidth*iHeight + 1078);
int iBMPLen = 0;
int nRet = RawToBMP(pRawBuf, iWidth, iHeight, iChannels, pBMPBuf,&iBMPLen);
if (nRet != 0)
{
free(pBMPBuf);
return nRet;
}
nRet = SaveData(szFileName, pBMPBuf, iBMPLen);
free(pBMPBuf);
return nRet;
}