笔记
作业
将一张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>
int main(int argc, const char *argv[])
{
//先以只读形式打开图片,获取图片信息
FILE *fp=fopen("./02 - 副本.bmp","r");
int bmp_size=0;
int width=0;
int height=0;
fseek(fp,2,SEEK_SET);
fread(&bmp_size,4,1,fp);
printf("图片大小为:%d\n",bmp_size);
fseek(fp,18,SEEK_SET);
fread(&width,4,1,fp);
fread(&height,4,1,fp);
printf("图片的信息为:宽=%d 高=%d\n",width,height);
fclose(fp);
//然后,再以 r+ 的形式打开图片,修改图片颜色,若以 w
//形式打开,会清空图片信息
/*
unsigned char bgr01[3]={0,255,255};
unsigned char bgr02[3]={0,0,255};
unsigned char bgr03[3]={0,0,0};
*/
unsigned char bgr[3][3]={{0,255,255},{0,0,255},{0,0,0}};
fp=fopen("./02 - 副本.bmp","r+");
fseek(fp,54,SEEK_SET);
int h1=height/3;
//int h2=2*h1;
for(int i=0;i<3;i++)
{
for(int j=i*h1;j<h1*(1+i);j++)
{
for(int k=0;k<width;k++)
{
fwrite(bgr+i,3,1,fp);
}
}
}
/*
for(int i=0;i<width;i++)
{
for(int j=0;j<h1;j++)
{
fwrite(bgr,3,1,fp);
}
}
for(int i=0;i<width;i++)
{
for(int j=h1;j<h2;j++)
{
fwrite(bgr+1,3,1,fp);
}
}
for(int i=0;i<width;i++)
{
for(int j=h2;j<=height;j++)
{
fwrite(bgr+2,3,1,fp);
}
}
*/
fclose(fp);
return 0;
}