Linux拼接三张图

原理
在这里插入图片描述
代码

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BMPSIZE 54
#define W  561
#define H  264
#define DP 24

char head[BMPSIZE];
char body1[W*H*DP/8/3];
char body2[W*H*DP/8/3];
char body3[W*H*DP/8/3];
/*
read--->读进来,读到数组中去 
write-->写出去,从数组中写出去 
*/
int main()
{
    int fd1=open("1.bmp", O_RDONLY);
    int fd2=open("2.bmp", O_RDONLY);
    int fd3=open("3.bmp", O_RDONLY);
	int fd4=open("4.bmp", O_WRONLY|O_CREAT, 0666);
	
	read(fd1, head, sizeof(head));
	read(fd1, body1, sizeof(body1));
	lseek(fd2, BMPSIZE+W*H*DP/8/3, SEEK_SET);//重定位第二张图片三分之一处 
	read(fd2, body2, sizeof(body2));
	lseek(fd3, BMPSIZE+W*H*DP/8/3+W*H*DP/8/3, SEEK_SET);//重定位第三张图片三分之二处 
	read(fd3, body3, sizeof(body3));
	
	write(fd4, head, sizeof(head));
	write(fd4, body1, sizeof(body1));
	write(fd4, body2, sizeof(body2));
	write(fd4, body3, sizeof(body3));	
	
	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
	return 0;
}

图片信息
在这里插入图片描述
1.bmp
在这里插入图片描述
2.bmp
在这里插入图片描述
3.bmp
在这里插入图片描述
合成图片 4.bmp
在这里插入图片描述
封装

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BMPSIZE 54
#define W  561
#define H  264
#define DP 24

char head[BMPSIZE];
char body1[W*H*DP/8/3];
char body2[W*H*DP/8/3];
char body3[W*H*DP/8/3];
/*
read--->读进来,读到数组中去 
write-->写出去,从数组中写出去 
*/
int main(int argc, char *argv[]){
    if(5 != argc){
    	printf("Usage: %s <bmp1> <bmp2> <bmp3> <bmp4>\n", argv[0]);
    	return -1;
	}
	
    int fd1=open("argv[1]", O_RDONLY);
    if(-1 == fd1){
	printf("%s:",argv[1]);
    	perror("");
    	return -1;
	}
    int fd2=open("argv[2]", O_RDONLY);
    if(-1 == fd2){
    	perror("open file2");
    	return -1;
	}
    int fd3=open("argv[3]", O_RDONLY);
    if(-1 == fd3){
    	perror("open file3");
    	return -1;
	}
	int fd4=open("argv[4]", O_WRONLY|O_CREAT, 0666);
	if(-1 == fd4){
    	perror("open file4");
    	return -1;
	}
	
	read(fd1, head, sizeof(head));
	read(fd1, body1, sizeof(body1));
	lseek(fd2, BMPSIZE+W*H*DP/8/3, SEEK_SET);//重定位第二张图片三分之一处 
	read(fd2, body2, sizeof(body2));
	lseek(fd3, BMPSIZE+W*H*DP/8/3+W*H*DP/8/3, SEEK_SET);//重定位第三张图片三分之二处 
	read(fd3, body3, sizeof(body3));
	
	write(fd4, head, sizeof(head));
	write(fd4, body1, sizeof(body1));
	write(fd4, body2, sizeof(body2));
	write(fd4, body3, sizeof(body3));	
	
	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏小泡泡#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值