1.用fwrite和fread将任意bmp图片,修改成德国的国旗
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("1.bmp","r");
fseek(fp,2,SEEK_SET);
int bmp_size = 0;
fread(&bmp_size,4,1,fp);
printf("文件大小为 %d 字节\n",bmp_size);
double w = 0,h = 0;
fseek(fp,18,SEEK_SET);
fread(&w,4,1,fp);
fread(&h,4,1,fp);
printf("图像尺寸为:%g * %g\n",w,h);
fclose(fp);
fp = fopen("1.bmp","r+");
fseek(fp,54,SEEK_SET);
// bmp 图片默认像素格式是 bgr的
unsigned char pix1[3] = {0,255,255};
for(int i=1;i<=w;i++)
{
for(double j=1;j<=h/3;j++)
{
fwrite(pix1,3,1,fp);
}
}
unsigned char pix2[3] = {0,0,255};
for(int i=1;i<=w;i++)
{
for(double j=h/3;j<=h*2/3;j++)
{
fwrite(pix2,3,1,fp);
}
}
unsigned char pix3[3] = {0,0,0};
for(int i=1;i<=w;i++)
{
for(double j=h*2/3;j<=h;j++)
{
fwrite(pix3,3,1,fp);
}
}
fclose(fp);
return 0;
}
2.