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

猜你喜欢

转载自blog.csdn.net/weixin_43319452/article/details/112014334