1.
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.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("./7CDACB0E44DA186FF3B81958F52E442D.bmp","r");
//找到第一个像素点
fseek(fp,54,SEEK_SET);
//定义容器存储信息
unsigned char bgr1[3]={0,0,0};
fread(bgr1,3,1,fp);
printf("第一个像素的bgr的值%d %d %d\n",bgr1[0],bgr1[1],bgr1[2]);
//找到最后一个像素
int bmp_high=0,bmp_wide=0,bmp_size=0;
fseek(fp,18,SEEK_SET);
fread(&bmp_high,4,1,fp);
fread(&bmp_wide,4,1,fp);
bmp_size=bmp_wide*bmp_high;
printf("像素总个数为:%d\n",bmp_size);
fseek(fp,(54+3*bmp_size),SEEK_SET);
unsigned char bgr2[3]={0,0,0};
fread(bgr2,3,1,fp);
printf("最后一个像素的bgr的值%d %d %d\n",bgr2[0],bgr2[1],bgr2[2]);
fclose(fp);
return 0;
}
2.
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.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[])
{
//让文件以r+的形式打开保证写的时候不改变其他数据
FILE* fp=fopen("./7CDACB0E44DA186FF3B81958F52E442D(复件1).bmp","r+");
if(fp==NULL)
{
perror("fp wrong");
return -1;
}
int bmp_high=0;
int bmp_wide=0;
fseek(fp,18,SEEK_SET);
fread(&bmp_high,4,1,fp);
fread(&bmp_wide,4,1,fp);
int bmp_high2=bmp_high*2;
int bmp_wide2=bmp_wide*2;
//修改图片大小的像素
fseek(fp,18,SEEK_SET);
fwrite(&bmp_high2,4,1,fp);
fwrite(&bmp_wide2,4,1,fp);
//全改为黑色
unsigned char bgr[3]={255,255,255};
fseek(fp,54,SEEK_SET);
for(int i=bmp_high;i<bmp_high2;i++)
{
for(int j=bmp_wide;j<bmp_wide2;j++)
{
fwrite(bgr,3,1,fp);
}
}
return 0;
}
3.
#include <stdio.h>
typedef struct {
unsigned char b;
unsigned char g;
unsigned char r;
} RGB;
int main() {
// 打开 BMP 文件
FILE* fp = fopen("./2.bmp", "r+");
if (fp == NULL) {
perror("fp error");
return -1;
}
// 读取 BMP 文件头部信息
fseek(fp, 18, SEEK_SET);
int bmp_wide, bmp_high;
fread(&bmp_wide, 4, 1, fp); // 读取像素宽度
fread(&bmp_high, 4, 1, fp); // 读取像素高度
// 修改德国国旗的颜色
int third_height = bmp_high / 3;
int third_height2 = 2 * bmp_high / 3;
RGB black = {0, 0, 0}; // 黑色
RGB red = {0, 0, 255}; // 红色
RGB gold = {0, 255, 255}; // 金色
// 修改第一部分为黑色
fseek(fp, 54, SEEK_SET); // 跳过 BMP 文件头部
for (int i = 0; i < third_height * bmp_wide; i++) {
fwrite(&black, sizeof(RGB), 1, fp);
}
// 修改第二部分为红色
for (int i = 0; i < (third_height2 - third_height) * bmp_wide; i++) {
fwrite(&red, sizeof(RGB), 1, fp);
}
// 修改第三部分为金色
for (int i = 0; i < (bmp_high - third_height2) * bmp_wide; i++) {
fwrite(&gold, sizeof(RGB), 1, fp);
}
fclose(fp);
printf("Germany flag pattern applied to BMP file.\n");
return 0;
}