作业:
第一题:
FILE *fp=fopen("./shenying.bmp","r+");
int bmp_weight,bmp_high;
fseek(fp,18,SEEK_SET);
fread(&bmp_weight,4,1,fp);
fseek(fp,22,SEEK_SET);
fread(&bmp_high,4,1,fp);
printf("%d %d\n",bmp_weight,bmp_high);
fseek(fp,54,SEEK_SET);
for(int i=1;i<=bmp_weight;i++)
{
for(int j=1;j<=bmp_high;j++)
{
unsigned char bgr[3]={0,0,0};
fread(bgr,3,1,fp);
if(i==1&&j==1 || i==bmp_weight&&j==bmp_high)
printf("%d,%d,%d\n",bgr[0],bgr[1],bgr[2]);
}
}
fclose(fp);
第二题:
FILE *fp2=fopen("./shenying.bmp","r+");
fseek(fp2,18,SEEK_SET);
fread(&bmp_weight,4,1,fp2);
fseek(fp2,18,SEEK_SET);
int new_weight=bmp_weight*2;
fwrite(&new_weight,4,1,fp2);
fseek(fp2,22,SEEK_SET);
fread(&bmp_high,4,1,fp2);
fseek(fp2,22,SEEK_SET);
int new_high=bmp_high*2;
fwrite(&new_high,4,1,fp2);
/*fseek(fp2,28,SEEK_SET);
char bitcount,newcount;
fread(&bitcount,1,1,fp2);
fseek(fp2,28,SEEK_SET);
newcount=bitcount*4;
fwrite(&newcount,1,1,fp2);*/
fseek(fp2,54,SEEK_SET);
unsigned char bgr[3];
for(int m=0;m<new_weight;m++)
{
for(int n=0;n<new_high;n++)
{
if(m<bmp_weight&&n<bmp_high)
{
fread(bgr,3,1,fp2);
fseek(fp2,54,SEEK_SET);
fwrite(bgr,3,1,fp2);
}
else
fseek(fp2,54,SEEK_SET);
fwrite(black,3,1,fp2);
}
}
fclose(fp2);
第三题:
FILE *fp1=fopen("./rising_freedom.bmp","r+");
fseek(fp1,18,SEEK_SET);
fread(&bmp_weight,4,1,fp1);
fseek(fp1,22,SEEK_SET);
fread(&bmp_high,4,1,fp1);
unsigned char black[3]={0,0,0};
unsigned char red[3]={0,0,255};
unsigned char yellow[3]={0,255,255};
fseek(fp1,54,SEEK_SET);
for(int i=0;i<bmp_weight;i++)
{
for(int j=0;j<bmp_high;j++)
{
if(i<=bmp_weight/3)
fwrite(yellow,3,1,fp1);
else if(i>(bmp_weight/3) && i<=((2*bmp_weight)/3))
fwrite(red,3,1,fp1);
else
fwrite(black,3,1,fp1);
}
}
fclose(fp1);
思维导图: