bmp格式图片文件读取

时间:2022-05-02 16:59:10

C++读取bmp图片的例子

 #include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> #define WIDTHBYTES(x) ((x+31)/32*4)
#define my(x) ( ( (x + 31) & ~31) / 8) BITMAPFILEHEADER bf; //BMP文件头结构体
BITMAPINFOHEADER bi; //BMP信息头结构体 int i,j;
RGBQUAD *ipRGB; //颜色表
DWORD NumColors; //颜色表数目
unsigned char ** Imgdata; void read(char*);
void display();
void org(char*);
void gray(char*); void test()
{
DWORD LineByte;
DWORD ImgSize;
LineByte=(DWORD)my(bi.biWidth*bi.biBitCount); //计算位图的实际宽度并确保它为32的倍数
ImgSize=(DWORD)LineByte*bi.biHeight;
printf("LineByte is %d. \n", LineByte);
printf("ImgSize is %d. \n", ImgSize);
} int main()
{
char *filename;
filename="U616.bmp";
read(filename);
display();
test();
org("123.bmp");
free(Imgdata);
// printf("<2>");
// display();
free(ipRGB);
system("pause");
return ;
} void read(char* filename)
{
FILE* fp;
fp=fopen(filename,"rb");
if(fp == NULL)
{
printf("Open file error!");
exit();
} //读取信息头、文件头
fread(&bf,sizeof(BITMAPFILEHEADER),,fp);
fread(&bi,sizeof(BITMAPINFOHEADER),,fp); if (bi.biClrUsed != )
NumColors=(DWORD)bi.biClrUsed;
else
switch (bi.biBitCount)
{
case : NumColors=; break;
case : NumColors=; break;
case : NumColors=; break;
case : NumColors=; break;
} //分配调色板内存
ipRGB=(RGBQUAD *)malloc(NumColors*sizeof(RGBQUAD));
fread(ipRGB,sizeof(RGBQUAD),NumColors,fp); Imgdata=new unsigned char*[bi.biHeight]; //声明一个指针数组
if(bi.biBitCount==)
{
for ( i=(bi.biHeight)-;i>=;i--)
Imgdata[i]=new unsigned char[bi.biWidth*]; //每个数组元素也是一个指针数组 for ( i=(bi.biHeight)-;i>=;i--)
for(j=;j<bi.biWidth*;j++)
fread(&Imgdata[i][j],,,fp);//每次只读取一个1字节,存入数组
}
else
{
for ( i=(bi.biHeight)-;i>=;i--)
Imgdata[i]=new unsigned char[bi.biWidth]; for ( i=(bi.biHeight)-;i>=;i--)
for(j=;j<bi.biWidth;j++)
fread(&Imgdata[i][j],,,fp);
} fclose(fp);
} void org(char *filename)
{
FILE* fp;
fp=fopen(filename,"wb");
fwrite(&bf,sizeof(BITMAPFILEHEADER),,fp); //写文件头
fwrite(&bi,sizeof(BITMAPINFOHEADER),,fp); //写信息头
fwrite(ipRGB,sizeof(RGBQUAD),NumColors,fp);//写颜色头 if(bi.biBitCount==)
for (i=(bi.biHeight)- ;i>=;i--)
for (j= ;j<my(bi.biWidth*);j++)
fwrite(&Imgdata[i][j],,,fp);
else
for (i=(bi.biHeight)- ;i>=;i--)
for (j= ;j<bi.biWidth;j++)
fwrite(&Imgdata[i][j],,,fp);
fclose(fp);
} void gray(char *filename)
{
FILE* fp;
fp=fopen(filename,"wb");
/*
bf.bfOffBits=(DWORD)1078;
bi.biSizeImage=(DWORD)(bi.biWidth*bi.biHeight);
bf.bfSize=bi.biSizeImage+bf.bfOffBits;
bi.biBitCount=(WORD)8;
bi.biClrUsed=(DWORD)256;
bi.biClrImportant=(DWORD)256;
bi.biXPelsPerMeter=(LONG)0;
bi.biYPelsPerMeter=(LONG)0;
NumColors=(DWORD)bi.biClrUsed;
ipRGB=(RGBQUAD *)malloc(NumColors*sizeof(RGBQUAD));
*/
fwrite(&bf,sizeof(BITMAPFILEHEADER),,fp); //写文件头
fwrite(&bi,sizeof(BITMAPINFOHEADER),,fp); //写信息头
fwrite(ipRGB,sizeof(RGBQUAD),NumColors,fp);//写颜色头 char temp;
for (i=(bi.biHeight)- ;i>=;i--)
for (j= ;j<bi.biWidth*;j++)
if(j%==)
{
temp=(Imgdata[i][j]*+Imgdata[i][j+]*+Imgdata[i][j+]*)>>;
fwrite(&temp,,,fp);
}
else
{
fwrite(&temp,,,fp);
}
fclose(fp);
} void display()
{
printf("tagBITMAPFILEHEADER info is: %d\n",sizeof(BITMAPFILEHEADER));
printf("bfType is %d.\n", bf.bfType);
printf("bfSize is %d.\n", bf.bfSize);
printf("bfReserved1 is %d.\n", bf.bfReserved1);
printf("bfReserved2 is %d.\n", bf.bfReserved2);
printf("bfOffBits is %d.\n", bf.bfOffBits); printf("\ntagBITMAPINFOHEADER info is: %d\n",sizeof(BITMAPINFOHEADER));
printf("biSize is %d. \n", bi.biSize);
printf("biWidth is %ld.\n", bi.biWidth);
printf("biHeight is %ld.\n", bi.biHeight);
printf("biPlanes is %d. \n", bi.biPlanes);
printf("biBitCount is %d. \n", bi.biBitCount); //每像素所需位数
printf("biCompression is %d. \n", bi.biCompression);
printf("biSizeImage is %d. \n", bi.biSizeImage);
printf("biXPelsPerMerer is %ld.\n", bi.biXPelsPerMeter); //每米像素数
printf("biYPelsPerMerer is %ld.\n", bi.biYPelsPerMeter);
printf("biClrUsed is %d. \n", bi.biClrUsed);
printf("biClrImportant is %d. \n", bi.biClrImportant);
printf("\nNum of colors is %d. \n", NumColors);
}