所有人,今日作业:用fwrite 和 fseek功能,将一张bmp格式的图片更改成 德国国旗
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./rising_freedom(复件).bmp","r");
fseek(fp,2,SEEK_SET);
int bmp_size=0;
fread(&bmp_size,4,1,fp);
printf("%d\n",bmp_size);
int bmp_w=0;
int bmp_h=0;
fseek(fp,18,SEEK_SET);
fread(&bmp_w,4,1,fp);
fread(&bmp_h,4,1,fp);
printf("图像像素信息为: %d*%d\n",bmp_w,bmp_h);
fclose(fp);
FILE* fp2=fopen("./rising_freedom(复件).bmp","r+");
fseek(fp2,54,SEEK_SET);
unsigned char bgr1[3]={0,0,0};//black
unsigned char bgr2[3]={0,0,255};//red
unsigned char bgr3[3]={0,255,255};//yellow
for(int j=0;j<bmp_h;j++){
for(int i=0;i<bmp_w;i++){
if(j>=0&&j<(bmp_h/3)){
fwrite(bgr1,3,1,fp2);
}
if(j>=(bmp_h/3)&&j<(bmp_h/3*2)){
fwrite(bgr2,3,1,fp2);
}
if(j>=(bmp_h/3*2)&&j<bmp_h){
fwrite(bgr3,3,1,fp2);
}
}
}
fclose(fp2);
return 0;
}