寻找一个bmp图片,输出这张bmp图片第一个像素点和最后一个像素点的bgr的值
#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, char const *argv[])
{
FILE* fp=fopen("/mnt/hgfs/Linux_share/rising_freedom/rising_freedom.bmp","r+");
int bmp_w=0,bmp_h=0;
fseek(fp,18,SEEK_SET);
fread(&bmp_w,4,1,fp);
fread(&bmp_h,4,1,fp);
fseek(fp, 54, SEEK_SET);
// 读取第一个像素点(注意BMP是BGR顺序)
unsigned char bgr[3];
fread(bgr, 1, 3, fp);
printf("First pixel BGR: %u, %u, %u\n", bgr[0], bgr[1], bgr[2]);
// 计算最后一个像素点的位置
fseek(fp, 54 + (bmp_w * 3 * (bmp_h - 1)) + (bmp_w * 3 - 3), SEEK_SET);
// 读取最后一个像素点
fread(bgr, 1, 3, fp);
printf("Last pixel BGR: %u, %u, %u\n", bgr[0], bgr[1], bgr[2]);
return 0;
}
修改一张bmp图片的大小,让他的宽度*2,高度*2,也就是最终像素点的数量*4,最后使用黑色像素点填充图片扩大的部分(不要求效果,只要求代码)
#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>
#include <stdio.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(void)
{
FILE *fp=fopen("/mnt/hgfs/Linux_share/rising_freedom/rising_freedom.bmp","r+");
int bmp_w=0,bmp_h=0;
fseek(fp,18,SEEK_SET);
fread(&bmp_w,4,1,fp);
fread(&bmp_h,4,1,fp);
int a=bmp_w*2;
int b=bmp_h*2;
fseek(fp,18,SEEK_SET);
fwrite(&a,4,1,fp);
fwrite(&b,4,1,fp);
unsigned char bgr[3]={0,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<bmp_h;i++)
{
for(int j=bmp_w;j<a;j++)
{
fwrite(bgr,3,1,fp);
}
}
for (int i= bmp_h; i < b; i++)
{
for(int j=0;j<a;j++)
{
fwrite(bgr,3,1,fp);
}
}
fclose(fp);
return 0;
}
将一张bmp图片修改成德国的国旗(如下图)
#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, char const *argv[])
{
FILE *fp = fopen("/mnt/hgfs/Linux_share/rising_freedom/rising_freedom.bmp", "r+");
fseek(fp,18,SEEK_SET);
int bmp_w=0,bmp_h=0;
fread(&bmp_w,4,1,fp);
fread(&bmp_h,4,1,fp);
int b = bmp_h/3;
unsigned char bgr[3]={0,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<b;i++)
{
for(int j=0;j<bmp_w;j++)
{
fwrite(bgr,3,1,fp);
}
}
unsigned char bgr1[3]={0,0,255};
for(int i=b;i<b*2;i++)
{
for(int j=0;j<bmp_w;j++)
{
fwrite(bgr1,3,1,fp);
}
}
unsigned char bgr2[3]={255,255,0};
for(int i=b*2;i<bmp_h;i++)
{
for(int j=0;j<bmp_w;j++)
{
fwrite(bgr2,3,1,fp);
}
}
return 0;
}