计算bmp位图的大小的方法
说明:
1,当bmp为24位位图时:文件头的大小为14B;位图信息头的大小为40B;没有颜色表;数据大小=每行字节数*行数(每行所用的在字节数为(3*Width+3)/4*4;
2,当bmp为小于24位位图时:文件头的大小为14B;位图信息头的大小为40B;有颜色表,大小为2的位数次方(2^1;2^4;2^8);数据大小=每行字节数*行数(每行所用的字节数:(Width * BitCount/8+3)/4*4);
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>
#include <TlHelp32.h>
#include <Psapi.h>
bool saveBmp(char *bmpName, unsigned char
*imgBuf, int width, int height,
int biBitCount, RGBQUAD *pColorTable);
int main()
{
FILE *infile=NULL;
FILE *outfile=NULL;
BITMAPFILEHEADER header;
BITMAPINFOHEADER infoheader;
RGBQUAD rgbquad;
infile=fopen("c://t.bmp","rb");
int tt= fread(&header,sizeof(header),1,infile);
tt=fread(&infoheader,sizeof(infoheader),1,infile);
long bmpWith=infoheader.biWidth;
long bmpHight=infoheader.biHeight;
WORD bmpBitCount=infoheader.biBitCount;
if (infoheader.biBitCount==1)
{
RGBQUAD pColorTable[2];
fread(pColorTable,sizeof(RGBQUAD),2,infile);
pColorTable[0].rgbBlue=255-pColorTable[0].rgbBlue;
pColorTable[0].rgbGreen=255-pColorTable[0].rgbGreen;
pColorTable[0].rgbRed=255-pColorTable[0].rgbRed;
pColorTable[1].rgbBlue=255-pColorTable[0].rgbBlue;
pColorTable[1].rgbGreen=255-pColorTable[0].rgbGreen;
pColorTable[1].rgbRed=255-pColorTable[0].rgbRed;
int lineByte=(infoheader.biWidth * infoheader.biBitCount/8+3)/4*4;
unsigned char* pBmpBuf=new unsigned char[lineByte * infoheader.biHeight];
fread(pBmpBuf,1,lineByte*infoheader.biHeight,infile);
Sleep(1000);
bmpHight=bmpHight-20;
saveBmp("c://t2.bmp",pBmpBuf,bmpWith,bmpHight,bmpBitCount,pColorTable);
}
fclose(infile);
infile=NULL;
outfile=NULL;
return 0;
}
/*****************************************
* 函数名称:
* saveBmp()
*
*函数参数:
* char *bmpName-文件名字及路径
* unsigned char *imgBuf-待存盘的位图数据
* int width-以像素为单位待存盘位图的宽
* int height-以像素为单位待存盘位图高
* int biBitCount-每像素所占位数
* RGBQUAD *pColorTable-颜色表指针
*返回值:
* 0为失败,1为成功
*
*说明:给定一个图像位图数据、宽、高、颜色表
指针及每像素所占的位数等信息,
* 将其写到指定文件中
******************************************
*****************************/
bool saveBmp(char *bmpName, unsigned char
*imgBuf, int width, int height,
int biBitCount, RGBQUAD *pColorTable)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!imgBuf)
return 0;
//颜色表大小,以字节为单位,灰度图像颜色表
//为1024字节,彩色图像颜色表大小为0
int colorTablesize=0;
if(biBitCount==1)
colorTablesize=8;
//待存储图像数据每行字节数为4的倍数
int lineByte=(width * biBitCount/8+3)/4*4;
//以二进制写的方式打开文件
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize= sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER)
+ colorTablesize + lineByte*height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bfOffBits=54+colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
//写位图信息头进内存
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);
//如果灰度图像,有颜色表,写入文件
if(biBitCount==1)
fwrite(pColorTable, sizeof(RGBQUAD),2, fp);
//写位图数据进文件
fwrite(imgBuf, height*lineByte, 1, fp);
//关闭文件
fclose(fp);
return 1;
}