📄 flip.c
字号:
/*----------------------------------------------------------------------PROGRAM: flip.cDATE: 7/7/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This routine reads in an ASCII list file of the format filename . . . from the input directory and flip each image in the indir. It outputs the flipped files in an output directory with the same names---------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <float.h>#include "util.h"#include "io.h"/* ----------- Command-Line Parsing Stuff ------- */extern int optind;extern char *optarg;char *progname; /* used to store the name of the program */char comline[256]; /* used to store the entire command line */#define OPTIONS "i:l:f:o:"char *usage = "-i indir -l list -f h/v -o outdir \n";char *help="\Flips Input Images (horizontally/vertically)\n\n\-i indir \t input directory\n\-l list \t ASCII file listing indir files to process (one per line)\n\-f flip \t h (horizontal), v (vetical)\n\-o outdir \t output directory\n";#define MAX_CHARS 256/* ---------- Function prototypes --------- */void flip_horizontal(float **image_in, float**image_out, int nrow, int ncol);void flip_vertical(float **image_in, float**image_out, int nrow, int ncol);/*----------------------------------------------------------------------*/main(int argc, char **argv){ register int i,j,k,l,ii,jj; int f, c, nframe, nfeatures, sets, bytes_pixel; int nrow, ncol, M, N; char command[MAX_CHARS],indir[MAX_CHARS],listfile[MAX_CHARS], \ infile[MAX_CHARS], maskfile[MAX_CHARS],outdir[MAX_CHARS], \ filename[MAX_CHARS], line[MAX_CHARS], flip[MAX_CHARS]; float **image_in, **image_out; unsigned char **char_image; FILE *fp; /* required input flags */ int errflag = 0; int inflag = 0; int listflag = 0; int flipflag = 0; int outflag = 0; int do_horizontal = 0; int do_vertical = 0; progname = argv[0]; for (i=0; i<argc; i++) strcat(comline, argv[i]),strcat(comline, " "); /* ---------------------- Command Line Parse ------------------------ */ while ((c = getopt(argc, argv, OPTIONS)) != EOF) switch (c) { case 'i': strcpy(indir, optarg); inflag = 1; break; case 'l': strcpy(listfile, optarg); listflag = 1; break; case 'f': strcpy(flip, optarg); flipflag = 1; break; case 'o': strcpy(outdir, optarg); outflag = 1; break; case '?': errflag = 1; break; } /* --- command line error check --- */ if (errflag || !inflag || !listflag || !flipflag) { fprintf(stderr,"\nUSAGE: %s %s\n%s\n", progname, usage, help); exit(1); } if (strncmp(flip, "h", 1) == 0) do_horizontal = 1; else if (strncmp(flip, "v", 1) == 0) do_vertical = 1; else { fprintf(stderr, "\nERROR: flip option: %s, is invalid\n"); exit(2); } /* ---- read indir descriptor ---- */ read_descriptor(indir, &nframe, &sets, &bytes_pixel, &ncol, &nrow); if (sets>1) myerror("Input files must be single-set DAT files!"); image_in = matrix(1, nrow, 1, ncol); image_out = matrix(1, nrow, 1, ncol); if (bytes_pixel==1) char_image = cmatrix(1, nrow, 1, ncol); /* ---- loop over input list file and flip ------- */ if ((fp = fopen(listfile, "r")) == NULL) { fprintf(stderr,"ERROR: Could not open input file %s \n\n", listfile); exit(1); } nframe = 0; while (fgets(line, MAX_CHARS, fp)) { if (strncmp(line, "#", 1) != 0 && strlen(line)>1) { nframe++; /* ---- read next image ---- */ sscanf(line,"%s",infile); sprintf(filename,"%s/%s", indir, infile); if (bytes_pixel == 4) read_RAW_float(filename, image_in, nrow, ncol); else read_RAW(filename, image_in, nrow, ncol); fprintf(stdout,"Read %d-by-%d %s input file %s\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), filename); /* --- flip the image --- */ if (do_horizontal == 1) flip_horizontal(image_in, image_out, nrow, ncol); if (do_vertical == 1) flip_vertical(image_in, image_out, nrow, ncol); if (bytes_pixel == 1) for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) char_image[i][j] = (int) image_out[i][j]; /* --- write output file --- */ sprintf(filename,"%s/%s", outdir, infile); if (bytes_pixel==4) write_RAW_float(filename, image_out, nrow, ncol); else write_RAW(filename, char_image, nrow, ncol); fprintf(stdout,"Wrote %d-by-%d %s output file %s\n\n", nrow, ncol, (bytes_pixel==4 ? "float":"uchar"), filename); } } /* --- write output descriptor ------ */ write_descriptor(outdir, nframe, ncol, nrow, bytes_pixel, comline); free_matrix(image_in, 1, nrow, 1, ncol); free_matrix(image_out, 1, nrow, 1, ncol); if (bytes_pixel==1) free_cmatrix(char_image, 1, nrow, 1, ncol); fclose(fp); return 0;}/* ====================================================================== */void flip_horizontal(float **image_in, float **image_out, int nrow, int ncol){ register int i,j; for (i=1; i<=nrow; i++) for (j=1; j<=ncol; j++) image_out[i][j] = image_in[i][ncol-j+1];}/* ---------------------------------------------------------------------- */void flip_vertical(float **image_in, float **image_out, int nrow, int ncol){ register int i,j; for (j=1; j<=ncol; j++) for (i=1; i<=nrow; i++) image_out[i][j] = image_in[nrow-i+1][j];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -