8.6 Day 2(IO进程线程)

一、作业

第一题: 寻找一个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;
}

结果:

第二题: 修改一张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图片修改成德国的国旗(如下图)

#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;
}

结果:

二、思维导图

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值