一、作业
第一题: 寻找一个bmp图片,输出这张bmp图片第一个像素点和最后一个像素点的bgr的值
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./rising_freedom.bmp","r");
int bmp_wide = 0, bmp_high = 0;
//定位到存放文件宽度和文件高度的位置
fseek(fp,18,SEEK_SET);
fread(&bmp_wide,4,1,fp);//读取文件的宽度
fread(&bmp_high,4,1,fp);//读取文件的高度
printf("图像的格式为%d * %d\n",bmp_wide,bmp_high);
fclose(fp);
FILE *fp1 = fopen("./rising_freedom.bmp","r+");
unsigned char picture[3]={0};
fseek(fp,54,SEEK_SET);//定位到存放像素点的位置
fread(picture,3,1,fp);
printf("%d %d %d\n",picture[0],picture[1],picture[2]);
fseek(fp,54+(bmp_wide*bmp_high)*3-1,SEEK_SET);//定位到像素点的末尾位置
fread(picture,3,1,fp);
printf("%d %d %d\n",picture[0],picture[1],picture[2]);
fclose(fp1);
return 0;
}
结果:
![](https://i-blog.csdnimg.cn/direct/a15b248a5f4f4a0aa6bdbcfcd3679cbb.png)
第二题: 修改一张bmp图片的大小,让他的宽度*2,高度*2,也就是最终像素点的数量*4,最后使用黑色像素点填充图片扩大的部分(不要求效果,只要求代码)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./rising_freedom.bmp","r");
int bmp_wide=0,bmp_high=0;
int temp = 54;
fseek(fp,18,SEEK_SET);
fread(&bmp_wide,4,1,fp);
fread(&bmp_high,4,1,fp);
printf("宽:%d 高:%d",bmp_wide,bmp_high);
fclose(fp);
FILE *fp1 = fopen("./rising_freedom.bmp","r+");
unsigned char black[3]={0,0,25};
fseek(fp,18,SEEK_SET);
bmp_wide = bmp_wide * 2;
fwrite(&bmp_wide,4,1,fp1);
bmp_high = bmp_high * 2;
fwrite(&bmp_high,4,1,fp1);
fseek(fp,temp,SEEK_SET);
for(int i=0; i<bmp_wide; i++)
{
for(int j=0; j<bmp_high; j++)
{
if(i<(bmp_wide) && j<(bmp_high))
{
temp++;
}
else
{
fwrite(black,sizeof(black),1,fp1);
}
}
}
fclose(fp1);
return 0;
}
第三题: 将一张bmp图片修改成德国的国旗(如下图)
![](https://i-blog.csdnimg.cn/direct/74135fc08c454f399e710e1d83a79cb9.png)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./rising_freedom.bmp","r");
int bmp_wide=0,bmp_high=0;
int temp = 54;
fseek(fp,18,SEEK_SET);
fread(&bmp_wide,4,1,fp);
fread(&bmp_high,4,1,fp);
printf("宽:%d 高:%d",bmp_wide,bmp_high);
fclose(fp);
FILE *fp1 = fopen("./rising_freedom.bmp","r+");
unsigned char b[3]={0,0,0}, r[3]={0,0,255},y[3]={0,255,255};
fseek(fp,54,SEEK_SET);
for(int i=0; i<bmp_high; i++)
{
for(int j=0; j<bmp_wide; j++)
{
if(i>=0 && i<bmp_high/3)
{
fwrite(y,sizeof(y),1,fp1);
}
else if(i>=bmp_high/3 && i<bmp_high/3*2)
{
fwrite(b,sizeof(b),1,fp1);
}
else
{
fwrite(r,sizeof(r),1,fp1);
}
}
}
return 0;
}
结果:
![](https://i-blog.csdnimg.cn/direct/bb19c1a140524ee2916e5910a9ade935.png)
二、思维导图