标准IO函数-将bmp图片修改为德国国旗样式

代码:
 

#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>
int main(int argc, const char *argv[])
{
	int size_bmp=0;
	int length_bmp=0;
	int hight_bmp=0;
	FILE* fp=fopen(argv[1],"r");
	fseek(fp,2,SEEK_SET);//偏移2,找到文件写入大小的位置;
	fread(&size_bmp,1,4,fp);
	fseek(fp,18,SEEK_SET);//找到文件写入图片宽度位置
	fread(&length_bmp,1,4,fp);
	//fseek(fp,22,SEEK_SET);
	fread(&hight_bmp,1,4,fp);//此时光标已经到23的位置,从该位置读取图片的高
	printf("%d\n%d\n%d\n",size_bmp,length_bmp,hight_bmp);
	fclose(fp);
	//像素点总数为length_bmp*hight_bmp=1330155
	//从54位后(不含54)是存放像素点的信息,所以像素点存放的总字节大小为size_bmp-54=3993120
	//则每个像素点的字节大小为像素总字节/像素点总数=3993120/1330155=3
	unsigned char red[3]={0,0,255};
	unsigned char yellow[3]={0,255,255};
	unsigned char black[3]={0,0,0};
	fp=fopen(argv[1],"r+");//防止第二次打开时把图片文件内容清空
	fseek(fp,54,SEEK_SET);//使光标指向像素点开始的位置
	int i;
	for(i=0;i<hight_bmp;i++){
		for(int j=0;j<length_bmp;j++){
			if(i<=(hight_bmp/3)){
				fwrite(yellow,3,1,fp);
			}
			else if(i>(hight_bmp/3) && i<=(hight_bmp*2/3)){
				fwrite(red,3,1,fp);
			}
			else{
				fwrite(black,3,1,fp);
			}
		}
	}
	fclose(fp);
	return 0; 
}

运行结果:
原图:

修改后:

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值