C语言bmp文件处理函数 c语言文件rb+

如何用C语言编程来显示一个bmp文件

直接上代码吧:

为德惠等地区用户提供了全套网页设计制作服务,及德惠网站建设行业解决方案。主营业务为网站设计制作、做网站、德惠网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

#include Windows.h

#include stdio.h

#include string.h

#include malloc.h

unsigned char *pBmpBuf;//读入图像数据的指针

int bmpWidth;//图像的宽

int bmpHeight;//图像的高

RGBQUAD *pColorTable;//颜色表指针

int biBitCount;//图像类型,每像素位数

bool readBmp(char *bmpName)

{

//二进制读方式打开指定的图像文件

FILE *fp=fopen(bmpName,"rb");

if(fp==0) return 0;

//跳过位图文件头结构BITMAPFILEHEADER

fseek(fp, sizeof(BITMAPFILEHEADER),0);

//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中

BITMAPINFOHEADER head;

fread(head, sizeof(BITMAPINFOHEADER), 1,fp);

//获取图像宽、高、每像素所占位数等信息

bmpWidth = head.biWidth;

bmpHeight = head.biHeight;

biBitCount = head.biBitCount;

//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)

int lineByte=(bmpWidth * biBitCount/8+3)/4*4;

//灰度图像有颜色表,且颜色表表项为256

if(biBitCount==8){

//申请颜色表所需要的空间,读颜色表进内存

pColorTable=new RGBQUAD[256];

fread(pColorTable,sizeof(RGBQUAD),256,fp);

}

//申请位图数据所需要的空间,读位图数据进内存

pBmpBuf=new unsigned char[lineByte * bmpHeight];

fread(pBmpBuf,1,lineByte * bmpHeight,fp);

//关闭文件

fclose(fp);

return 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==8)

colorTablesize=1024;

//待存储图像数据每行字节数为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==8)

fwrite(pColorTable, sizeof(RGBQUAD),256, fp);

//写位图数据进文件

fwrite(imgBuf, height*lineByte, 1, fp);

//关闭文件

fclose(fp);

return 1;

}

int main()

{

char inFileName[90],outFileName[90];

printf("请输入原始位图文件的文件名:");

scanf("%s",inFileName);

printf("请输入加密程序产生的新位图文件的文件名:");

scanf("%s",outFileName);

//读入指定BMP文件进内存

readBmp(inFileName);

//输出图像的信息

printf("width=%d,height=%d, biBitCount=%d\n",bmpWidth,bmpHeight, biBitCount);

//将图像数据存盘

saveBmp(outFileName, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);

//清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间

delete []pBmpBuf;

if(biBitCount==8)

delete []pColorTable;

return 0;

}

求处理图片bmp文件的程序代码(C语言)

bmp文件由文件头,位图信息头,颜色表,和数据部组成

文件头:

typedef struct tagBITMAPFILEHEADER

{

WORD bfType;//位图文件的类型,必须为BM(1-2字节)

DWORD bfSize;//位图文件的大小,以字节为单位(3-6字节,低位在前)

WORD bfReserved1;//位图文件保留字,必须为0(7-8字节)

WORD bfReserved2;//位图文件保留字,必须为0(9-10字节)

DWORD bfOffBits;//位图数据的起始位置,以相对于位图(11-14字节,低位在前)

//文件头的偏移量表示,以字节为单位

}BITMAPFILEHEADER;

位图信息头:

typedefstructtagBITMAPINFOHEADER{

DWORDbiSize;//本结构所占用字节数(15-18字节)

LONGbiWidth;//位图的宽度,以像素为单位(19-22字节)

LONGbiHeight;//位图的高度,以像素为单位(23-26字节)

WORDbiPlanes;//目标设备的级别,必须为1(27-28字节)

WORDbiBitCount;//每个像素所需的位数,必须是1(双色),(29-30字节)

//4(16色),8(256色)16(高彩色)或24(真彩色)之一

DWORDbiCompression;//位图压缩类型,必须是0(不压缩),(31-34字节)

//1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一

DWORDbiSizeImage;//位图的大小(其中包含了为了补齐行数是4的倍数而添加的空字节),以字节为单位(35-38字节)

LONGbiXPelsPerMeter;//位图水平分辨率,每米像素数(39-42字节)

LONGbiYPelsPerMeter;//位图垂直分辨率,每米像素数(43-46字节)

DWORDbiClrUsed;//位图实际使用的颜色表中的颜色数(47-50字节)

DWORDbiClrImportant;//位图显示过程中重要的颜色数(51-54字节)

}BITMAPINFOHEADER;

打开文件读出这些信息,然后用一个RGB的构造体二维数组(height*width)来读取数据部分

用c语言读取24位位图bmp文件

可以使用C语言标准函数库中的fopen、fseek、fclose等系列函数来打开bmp位图文件,以及进行相应的处理,下面是一个demo,仅供参考。以下代码在vc6.0中编译通过。

#include stdio.h

#include stdlib.h

#define BITMAPFILEHEADERLENGTH 14   // The bmp FileHeader length is 14

#define BM 19778                    // The ASCII code for BM

/* Test the file is bmp file or not */

void bmpFileTest(FILE* fpbmp);

/* To get the OffSet of header to data part */

void bmpHeaderPartLength(FILE* fpbmp);

/* To get the width and height of the bmp file */

void BmpWidthHeight(FILE* fpbmp);

//get r,g,b data

void bmpDataPart(FILE* fpbmp);

// output data to corresponding txt file

void bmpoutput(FILE *fpout);

unsigned int OffSet = 0;    // OffSet from Header part to Data Part

long width ;          // The Width of the Data Part

long height ;         // The Height of the Data Part

unsigned char r[2000][2000],output_r[2000][2000];

unsigned char g[2000][2000],output_g[2000][2000];

unsigned char b[2000][2000],output_b[2000][2000];

int main(int argc, char* argv[])

{

/* Open bmp file */

unsigned char *fp_temp;

FILE *fpbmp;

FILE *fpout;

fpbmp= fopen("1.bmp", "rb");

if (fpbmp == NULL)

{

printf("Open bmp failed!!!\n");

return 1;

}

fpout= fopen("out.bmp", "wb+");

if (fpout == NULL)

{

printf("Open out.bmp failed!!!\n");

return 1;

}

bmpFileTest(fpbmp);                //Test the file is bmp file or not

bmpHeaderPartLength(fpbmp);        //Get the length of Header Part

BmpWidthHeight(fpbmp);             //Get the width and width of the Data Part

//

fseek(fpbmp, 0L, SEEK_SET);

fseek(fpout, 0L, SEEK_SET);

fp_temp=(unsigned char *)malloc(OffSet);

fread(fp_temp, 1, OffSet, fpbmp);

fwrite(fp_temp,1,OffSet,fpout);

bmpDataPart(fpbmp);                //Reserve the data to file 

/*

如果您想对图片进行处理,请您再这里插入处理函数!!!!!!!!!!!!!!!!!!

*/

bmpoutput(fpout);

fclose(fpbmp);

fclose(fpout);

return 0;

}

void bmpoutput(FILE* fpout)

{

int i, j=0;

int stride;

unsigned char* pixout=NULL;

stride=(24*width+31)/8;

stride=stride/4*4;

pixout=(unsigned char *)malloc(stride);

fseek(fpout, OffSet, SEEK_SET);

for(j=0;jheight;j++)

{

for(i=0;iwidth;i++)

{

pixout[i*3+2]=output_r[height-1-j][i];

pixout[i*3+1]=output_g[height-1-j][i];

pixout[i*3]  =output_b[height-1-j][i];

}

fwrite(pixout, 1, stride, fpout);

}

}

void bmpDataPart(FILE* fpbmp)

{

int i, j=0;

int stride;

unsigned char* pix=NULL;

FILE* fpr;

FILE* fpg;

FILE* fpb;

if((fpr=fopen("bmpr.txt","w+")) == NULL)

{

printf("Failed to construct file bmpr.txt.!!!");

exit(1);

}

if((fpg=fopen("bmpg.txt","w+")) == NULL)

{

printf("Failed to construct file bmpg.txt.!!!");

exit(1);

}

if((fpb=fopen("bmpb.txt","w+")) == NULL)

{

printf("Failed to construct file bmpb.txt.!!!");

exit(1);

}

fseek(fpbmp, OffSet, SEEK_SET);

stride=(24*width+31)/8;

stride=stride/4*4;

pix=(unsigned char *)malloc(stride);

for(j=0;jheight;j++)

{

fread(pix, 1, stride, fpbmp);

for(i=0;iwidth;i++)

{

r[height-1-j][i]   =pix[i*3+2];

g[height-1-j][i]   =pix[i*3+1];

b[height-1-j][i]   =pix[i*3];

output_r[height-1-j][i]   =pix[i*3+2];

output_g[height-1-j][i]   =pix[i*3+1];

output_b[height-1-j][i]   =pix[i*3];

}

}

for(i =0; i  height; i++)

{

for(j = 0; j  width-1; j++)

{   

fprintf(fpb,"%4d",b[i][j]);

fprintf(fpg,"%4d",g[i][j]);

fprintf(fpr,"%4d",r[i][j]);

}

fprintf(fpb,"%4d\n",b[i][j]);

fprintf(fpg,"%4d\n",g[i][j]);

fprintf(fpr,"%4d\n",r[i][j]);

}

fclose(fpr);

fclose(fpg);

fclose(fpb);

}

void bmpFileTest(FILE* fpbmp)

{     

unsigned short bfType = 0;

fseek(fpbmp, 0L, SEEK_SET);//seek_set 起始位置

fread(bfType, sizeof(char), 2, fpbmp);

if (BM != bfType)

{

printf("This file is not bmp file.!!!\n");

exit(1);

}

}

/* To get the OffSet of header to data part */

void bmpHeaderPartLength(FILE* fpbmp)

{

fseek(fpbmp, 10L, SEEK_SET);

fread(OffSet, sizeof(char), 4, fpbmp);  

printf("The Header Part is of length %d.\n", OffSet);

}

/* To get the width and height of the bmp file */

void BmpWidthHeight(FILE* fpbmp)

{

fseek(fpbmp, 18L, SEEK_SET);

fread(width, sizeof(char), 4, fpbmp);

fseek(fpbmp, 22L, SEEK_SET);

fread(height, sizeof(char), 4, fpbmp);

printf("The Width of the bmp file is %ld.\n", width);

printf("The Height of the bmp file is %ld.\n", height);

}

怎么用C语言中的fopen函数打开bmp格式的图像文件

1.图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。

2.例程:

#include stdlib.h

#include stdio.h

int main ()

{

FILE * fpPhoto, * fpText, * fpTarget ;

int     iRead ;

char    szBuf[100] ;

printf ("请输入第一个文件名(jpg):\n") ;

gets (szBuf) ;

fpPhoto = fopen (szBuf, "rb") ;

printf ("请输入第二个文件名(txt):\n") ;

gets (szBuf) ;

fpText = fopen (szBuf, "rb") ;

printf ("请输入目的文件名(jpg):\n") ;

gets (szBuf) ;

fpTarget = fopen (szBuf, "wb") ;

if (!fpPhoto || !fpText || !fpTarget)

{

printf ("打开文件失败!\n") ;

system("pause") ;

return -1 ;

}

while ((iRead = fread (szBuf, 1, sizeof (szBuf), fpPhoto))  0)

fwrite (szBuf, 1, iRead, fpTarget) ;

while ((iRead = fread (szBuf, 1, sizeof (szBuf), fpText))  0)

fwrite (szBuf, 1, iRead, fpTarget) ;

fclose (fpPhoto) ;

fclose (fpText) ;

fclose (fpTarget) ;

return 0 ;

}


网站标题:C语言bmp文件处理函数 c语言文件rb+
URL标题:http://ybzwz.com/article/dojcgos.html