BMP格式图像读取与存储

时间:2021-08-23 04:27:34
全局变量:
1 #include "stdafx.h"
#include <windows.h> /*BMP位图数据是4字节对齐*/
#define WIDTHBYTES(bits) ((DWORD)(((bits)+31) & (~31)) / 8)
#define WIDTHBYTES(bits) ((DWORD)((((bits)+31) / 32) * 4) unsigned char *gPBmpBuf ; //指向图像数据的指针
int gBmpWidth ; //图像的宽
int gBmpHeight; //图像的高
RGBQUAD *gPColorTable ; //颜色表指针
int gBiBitCount; //图像类型,每个像素的位数
char bmp[] = "E:/test_pic.bmp";
char saveBmp[] = "E:/save_pic.bmp";

读取bmp文件:

 bool loadBmp(char *bmpName)
{
/*以二进制方式打开bmp文件*/
FILE *fp = fopen(bmpName , "rb");
if(NULL == fp)
{
printf("open file \"%s\" failed \n" , bmpName);
return false ;
} /*跳过bmp文件结构体*/
fseek(fp , sizeof(BITMAPFILEHEADER),); /*读取bmp文件信息头*/
BITMAPINFOHEADER infoHead ;
fread(&infoHead , sizeof(BITMAPINFOHEADER), , fp); /*获取图像宽,高,像素位数*/
gBmpWidth = infoHead.biWidth ;
gBmpHeight = infoHead.biHeight;
gBiBitCount= infoHead.biBitCount; /*获取每行像素所占内存大小(必须为4的倍数)*/
int lineByte = (gBmpWidth*gBiBitCount/ + )/* ; /*灰度图像有颜色表,且颜色表表项为256,24-位真彩色图像就不使用彩色板*/
if( == gBiBitCount)
{
gPColorTable = (RGBQUAD *)malloc(sizeof(RGBQUAD)*);
fread(gPColorTable , sizeof(RGBQUAD) , , fp);
} /*申请位图数据空间,并将位图数据存放到内存*/
gPBmpBuf = (unsigned char *)malloc(sizeof(unsigned char)*gBmpHeight*lineByte);
fread(gPBmpBuf, ,gBmpHeight*lineByte,fp); fclose(fp); return true ;
}

保存bmp文件格式:

 bool storeBmp(char *bmpName , unsigned char *imgBuf , int width , int height,
int gBiBitCount , RGBQUAD *gPColorTable )
{
/*对位图数据进行判空操作*/
if(NULL == imgBuf)
{
return false ;
} /*根据像素位数,判断当前颜色表大小*/
int colorTableSize = ;
if(gBiBitCount == )
{
/*
1字节用于蓝色分量
1字节用于绿色分量
1字节用于红色分量
1字节用于填充符(设置为0)
*/
colorTableSize = ; /*4*256*/
} /*待存储图像数据每行像素的大小(4的倍数)*/
int lineByte = (gBmpWidth*gBiBitCount/ + )/* ; FILE *fp = fopen(bmpName , "wb");
if(NULL == fp)
{
printf("creat file failed !\n");
return false ;
} /*填写位图文件头结构体*/
BITMAPFILEHEADER fileHead ;
fileHead.bfType = 0x4D42 ; //bmp类型
fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
colorTableSize + lineByte*height ;
fileHead.bfReserved1 = ;
fileHead.bfReserved2 = ;
fileHead.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
colorTableSize ;
fwrite(&fileHead , sizeof(BITMAPFILEHEADER), , fp); /*填写位图信息结构体*/
BITMAPINFOHEADER infoHead ;
infoHead.biBitCount = gBiBitCount ;
infoHead.biClrImportant = ;
infoHead.biClrUsed = ;
infoHead.biCompression = ;
infoHead.biHeight = height ;
infoHead.biPlanes = ;
infoHead.biSize = ;
infoHead.biSizeImage = lineByte*height ;
infoHead.biWidth = width ;
infoHead.biXPelsPerMeter = ;
infoHead.biYPelsPerMeter = ;
fwrite(&infoHead , sizeof(BITMAPINFOHEADER), , fp); /*填写颜色表*/
if(gBiBitCount == )
{
fwrite(&gPColorTable , sizeof(RGBQUAD), , fp);
} /*写位图数据进文件*/
fwrite( imgBuf, height * lineByte, , fp ); fclose(fp); return true ;
}

main入口测试:

 int _tmain(int argc, _TCHAR* argv[])
{ bool ret = false ; ret |= loadBmp(bmp);
ret |= storeBmp(saveBmp,gPBmpBuf,gBmpWidth,gBmpHeight,gBiBitCount,gPColorTable); if(false == ret)
{
printf("excut fail! \n");
}
else if(true == ret)
{
printf("excut success!\n");
} if(gBiBitCount == )
{
free(gPColorTable);
} free(gPBmpBuf); return ;
}