游泳

纯C下的读写BMP文件代码

2019-09-13 19:53:40来源:励志吧0次阅读

纯C代码下读写BMP文件代码,简单易懂。

/**//*************************************************************

**Copyright(c)2007,Shanghai

**

**文件名称:read_write_bmp_with_C.c

**编译:VisualC++/ANSIC/ISOC++

**日期:2007.10.24

**************************************************************/

#include"stdio.h"

#include"stdlib.h"

#definePIXPLINE320

typedefstructtagRGBQUAD...{//定义每个像素的数据类型

unsignedcharrgbBlue;

unsignedcharrgbGreen;

unsignedcharrgbRed;

}RGBQUAD;

intbmp_read(unsignedchar*image,intxsize,intysize,char*filename)...{

charfname_bmp[128];

sprintf(fname_bmp,"%s.bmp",filename);

FILE*fp;

if(!(fp=fopen(fname_bmp,"rb")))

return-1;

unsignedcharheader[54];

fread(header,sizeof(unsignedchar),54,fp);

fread(image,sizeof(unsignedchar),(size_t)(long)xsize*ysize*3,fp);

fclose(fp);

return0;

}

intbmp_write(unsignedchar*image,intxsize,intysize,char*filename)...{

unsignedcharheader[54]=...{

0x42,0x4d,0,0,0,0,0,0,0,0,

54,0,0,0,40,0,0,0,0,0,0,0,0,0,0,0,1,0,24,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0

};

longfile_size=(long)xsize*(long)ysize*3+54;

header[2]=(unsignedchar)(file_size&0x000000ff);

header[3]=(file_size>>8)&0x000000ff;

header[4]=(file_size>>16)&0x000000ff;

header[5]=(file_size>>24)&0x000000ff;

longwidth=xsize;

header[18]=width&0x000000ff;

header[19]=(width>>8)&0x000000ff;

header[20]=(width>>16)&0x000000ff;

header[21]=(width>>24)&0x000000ff;

longheight=ysize;

header[22]=height&0x000000ff;

header[23]=(height>>8)&0x000000ff;

header[24]=(height>>16)&0x000000ff;

header[25]=(height>>24)&0x000000ff;

charfname_bmp[128];

sprintf(fname_bmp,"%s.bmp",filename);

FILE*fp;

if(!(fp=fopen(fname_bmp,"wb")))

return-1;

fwrite(header,sizeof(unsignedchar),54,fp);

fwrite(image,sizeof(unsignedchar),(size_t)(long)xsize*ysize*3,fp);

fclose(fp);

return0;

}

voidclonebmp(unsignedchar*image,intxsize,intysize)

...{

bmp_read(image,xsize,ysize,"orgbmp");//orgbmp为当前目录下的bmp文件名

bmp_write(image,xsize,ysize,"clone_bmp");//clone_bmp为克隆的bmp文件名

}

/**//****************************************************************************

*名称:youwritetobmp()

*功能:写入bmp文件

*入口参数:RGBQUAD*pixarr----要写入的像素数组指针,intxsize----图像宽度,intysize----图像高度,char*filename--图像名称

*出口参数:无

*返回值:-1:错误;0:正确

****************************************************************************/

intyouwritetobmp(RGBQUAD*pixarr,intxsize,intysize,char*filename)...{

unsignedcharheader[54]=...{

0x42,0x4d,0,0,0,0,0,0,0,0,

54,0,0,0,40,0,0,0,0,0,

0,0,0,0,0,0,1,0,24,0,

0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,

0,0,0,0

};

inti;

intj;

longfile_size=(long)xsize*(long)ysize*3+54;

header[2]=(unsignedchar)(file_size&0x000000ff);

header[3]=(file_size>>8)&0x000000ff;

header[4]=(file_size>>16)&0x000000ff;

header[5]=(file_size>>24)&0x000000ff;

longwidth;

if(!(xsize%4))width=xsize;

elsewidth=xsize+(4-xsize%4);//如不是4的倍数,则转换成4的倍数

header[18]=width&0x000000ff;

header[19]=(width>>8)&0x000000ff;

header[20]=(width>>16)&0x000000ff;

header[21]=(width>>24)&0x000000ff;

longheight=ysize;

header[22]=height&0x000000ff;

header[23]=(height>>8)&0x000000ff;

header[24]=(height>>16)&0x000000ff;

header[25]=(height>>24)&0x000000ff;

charfname_bmp[128];

sprintf(fname_bmp,"%s.bmp",filename);

FILE*fp;

if(!(fp=fopen(fname_bmp,"wb")))

return-1;

fwrite(header,sizeof(unsignedchar),54,fp);

RGBQUADzero=...{0,0,0};//不足字节,用zero填充

for(j=0;j<ysize;j++)...{

if(!(xsize%4))...{

for(i=0;i<xsize;i++)...{

fwrite(pixarr+i,sizeof(RGBQUAD),1,fp);

}

}

else

...{

for(i=0;i<xsize;i++)...{

fwrite(pixarr+i,sizeof(RGBQUAD),1,fp);

}

for(i=xsize;i<xsize+(4-xsize%4);i++)...{

fwrite(&zero,sizeof(RGBQUAD),1,fp);

}

}

}

fclose(fp);

return0;

}

intmain()...{

unsignedchar*image;

intxsize=320;

intysize=163;

RGBQUADpixarray[PIXPLINE];//一行像素值数组

image=(unsignedchar*)malloc((size_t)xsize*ysize*3);

if(image==NULL)

return-1;

clonebmp(image,xsize,ysize);//功能一:实现一副图像的拷贝

youwritetobmp(pixarray,xsize,ysize,"yourimage");//功能二:实现像素的精确绘制,像素值在pixarray数组中

free(image);

return-1;

}

查看本文来源

脑血栓是怎么引起的
儿童低烧不退的小秘诀
婴儿发烧推拿
什么中药可以治疗便秘
分享到: