用fwrite 和 fseek功能,将一张bmp格式的图片更改成 德国国旗
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./ceshi.bmp","r+");
if(NULL==fp){
perror("open error");
return -1;
}
//读取图片大小
fseek(fp,2,SEEK_SET);
int bmp_size=0;
fread(&bmp_size,4,1,fp);
printf("图片大小:%d\n",bmp_size);
//读取图片宽度
fseek(fp,18,SEEK_SET);
int bmp_w;
fread(&bmp_w,4,1,fp);
printf("图片宽度:%d\n",bmp_w);
//读取图片高度
int bmp_h;
fread(&bmp_h,4,1,fp);
printf("图片宽度:%d\n",bmp_h);
//定义一个像素点的大小
unsigned char bgr1[3]={0,255,255};
unsigned char bgr2[3]={0,0,255};
unsigned char bgr3[3]={0,0,0};
fseek(fp,54,SEEK_SET);
int num=bmp_w*bmp_h;
int count=0;
for(int i=0;i<bmp_w;i++){
for(int j=0;j<bmp_h;j++){
count++;
if(count<=num/3){
fwrite(bgr1,3,1,fp);
}
else if(count>num/3&&count<=num*2/3){
fwrite(bgr2,3,1,fp);
}
else if(count>num*2/3&&count<=num){
fwrite(bgr3,3,1,fp);
}
}
}
fclose(fp);
return 0;
}