LINUX,C语言双进程bmp(24位)图片打马赛克
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>int get_bmp_length(FILE* fpbmp);
int get_bmp_width(FILE* fpbmp);
long get_bmp_size(FILE* fpbmp);
int copy_file(FILE* src,FILE* des,int start,int len);
void one_mosaic_bmp(FILE* src,FILE* des,int start,int level,int width);
void full_mosaic_bmp(FILE* src,FILE* des,int start,int level,int width,int length);#define LEVEL_MOSAIC 10int main(int argc, char const *argv[])
{if(argc != 3){fprintf(stderr,"input args error\n");fprintf(stderr,"usage: ./a.out \n");exit(EXIT_FAILURE);}FILE *src = fopen(argv[1],"r");if (NULL == src){perror("fopen");}FILE *des = fopen(argv[2],"w");if (NULL == src){perror("fopen");}int bmp_length = get_bmp_length(src);int bmp_width = get_bmp_width(src);long bmp_size = get_bmp_size(src);int bytes = copy_file(src,des,0,54);printf("%d,%d,%ld,%d\n",bmp_length,bmp_width,bmp_size,bytes);pid_t pid = fork();if(pid < 0){perror("fork");}else if(pid == 0){full_mosaic_bmp(src,des,54,LEVEL_MOSAIC,bmp_width,bmp_length/2);printf("child copy bytes = %d\n",bytes);}else{full_mosaic_bmp(src,des,(bmp_size-54)/2,LEVEL_MOSAIC,bmp_width,bmp_length-bmp_length/2);wait(NULL);}return 0;
}int get_bmp_length(FILE* fpbmp)
{int bmp_length;fseek(fpbmp, 22, SEEK_SET);fread(&bmp_length, sizeof(char), 4, fpbmp); return bmp_length;
}
int get_bmp_width(FILE* fpbmp)
{int bmp_width;fseek(fpbmp, 18, SEEK_SET);fread(&bmp_width, sizeof(char), 4, fpbmp); return bmp_width;
}
long get_bmp_size(FILE* fpbmp)
{fseek(fpbmp,0,SEEK_END);long file_size = ftell(fpbmp);return file_size;
}
int copy_file(FILE* src,FILE* des,int start,int len)
{int c,count = 0;fseek(src,start,SEEK_SET);fseek(des,start,SEEK_SET);while(1){c = fgetc(src);if(fputc(c ,des) == EOF) break;count++;if(count >= len) break;}return count;
}
void one_mosaic_bmp(FILE* src,FILE* des,int start,int level,int width){int i,j;char buf[3]={0};fread(buf, sizeof(char), 3, src); for( i = 0;i < level;i++ ){for( j = 0;j < level;j++ ){fwrite(&buf, sizeof(char), 3, des); }fseek(des, (width-level)*3, SEEK_CUR);}
}
void full_mosaic_bmp(FILE* src,FILE* des,int start,int level,int width,int length){int i,j;fseek(src,start,SEEK_SET);fseek(des,start,SEEK_SET);for ( i = 0; i < length/level+1; i++){for ( j = 0; j < width/level+1; j++){one_mosaic_bmp(src,des,start,level,width);fseek(src, (level-1)*3, SEEK_CUR);fseek(des, level*3-level*width*3, SEEK_CUR);}fseek(src, (level-1)*width*3, SEEK_CUR);fseek(des, (level-1)*width*3, SEEK_CUR);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!