Back to index

radiance  4R0+20100331
pflip.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: pflip.c,v 2.9 2006/08/08 18:04:41 greg Exp $";
00003 #endif
00004 /*
00005  * flip picture file horizontally and/or vertically
00006  */
00007 
00008 #include <stdio.h>
00009 #include  <time.h>
00010 #include  <string.h>
00011 
00012 #include "platform.h"
00013 #include "color.h"
00014 #include "resolu.h"
00015 
00016 int    order;                      /* input orientation */
00017 int    xres, yres;                 /* resolution (scanlen, nscans) */
00018 
00019 long   *scanpos;                   /* scanline positions */
00020 
00021 int    fhoriz=0, fvert=0;          /* flip flags */
00022 
00023 int    correctorder = 0;           /* correcting orientation? */
00024 
00025 FILE   *fin;                       /* input file */
00026 
00027 char   *progname;
00028 
00029 
00030 static void memerr(void);
00031 static void scanfile(void);
00032 static void flip(void);
00033 
00034 
00035 static int
00036 neworder(void)                     /* figure out new order from old */
00037 {
00038        register int  no;
00039 
00040        if (correctorder)
00041               return(order);              /* just leave it */
00042        if ((no = order) & YMAJOR) {
00043               if (fhoriz) no ^= XDECR;
00044               if (fvert) no ^= YDECR;
00045        } else {
00046               if (fhoriz) no ^= YDECR;
00047               if (fvert) no ^= XDECR;
00048        }
00049        return(no);
00050 }
00051 
00052 int
00053 main(
00054        int    argc,
00055        char   *argv[]
00056 )
00057 {
00058        static char   picfmt[LPICFMT+1] = PICFMT;
00059        int    i, rval;
00060        SET_DEFAULT_BINARY();
00061        SET_FILE_BINARY(stdout);
00062        progname = argv[0];
00063 
00064        for (i = 1; i < argc; i++)
00065               if (!strcmp(argv[i], "-h"))
00066                      fhoriz++;
00067               else if (!strcmp(argv[i], "-v"))
00068                      fvert++;
00069               else if (!strcmp(argv[i], "-c"))
00070                      correctorder++;
00071               else
00072                      break;
00073        if (i >= argc || argv[i][0] == '-') {
00074               fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
00075                             progname);
00076               exit(1);
00077        }
00078        if ((fin = fopen(argv[i], "r")) == NULL) {
00079               fprintf(stderr, "%s: cannot open\n", argv[i]);
00080               exit(1);
00081        }
00082        if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) {
00083               fprintf(stderr, "%s: cannot open\n", argv[i+1]);
00084               exit(1);
00085        }
00086                                    /* transfer header */
00087        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
00088               fprintf(stderr, "%s: input not a Radiance picture\n",
00089                             progname);
00090               exit(1);
00091        }
00092        if (rval)
00093               fputformat(picfmt, stdout);
00094                                    /* add new header info. */
00095        printargs(i, argv, stdout);
00096        putchar('\n');
00097                                    /* get picture size */
00098        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
00099               fprintf(stderr, "%s: bad picture size\n", progname);
00100               exit(1);
00101        }
00102                                    /* write new picture size */
00103        fputresolu(neworder(), xres, yres, stdout);
00104                                    /* goto end if vertical flip */
00105        if (fvert)
00106               scanfile();
00107        flip();                            /* flip the image */
00108        exit(0);
00109 }
00110 
00111 
00112 static void
00113 memerr(void)
00114 {
00115        fprintf(stderr, "%s: out of memory\n", progname);
00116        exit(1);
00117 }
00118 
00119 
00120 static void
00121 scanfile(void)                            /* scan to the end of file */
00122 {
00123        extern long   ftell();
00124        COLR   *scanin;
00125        int    y;
00126 
00127        if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL)
00128               memerr();
00129        if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
00130               memerr();
00131        for (y = yres-1; y > 0; y--) {
00132               scanpos[y] = ftell(fin);
00133               if (freadcolrs(scanin, xres, fin) < 0) {
00134                      fprintf(stderr, "%s: read error\n", progname);
00135                      exit(1);
00136               }
00137        }
00138        scanpos[0] = ftell(fin);
00139        free((void *)scanin);
00140 }
00141 
00142 
00143 static void
00144 flip(void)                                /* flip the picture */
00145 {
00146        COLR   *scanin, *scanout;
00147        int    y;
00148        register int  x;
00149 
00150        if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
00151               memerr();
00152        if (fhoriz) {
00153               if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
00154                      memerr();
00155        } else
00156               scanout = scanin;
00157        for (y = yres-1; y >= 0; y--) {
00158               if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) {
00159                      fprintf(stderr, "%s: seek error\n", progname);
00160                      exit(1);
00161               }
00162               if (freadcolrs(scanin, xres, fin) < 0) {
00163                      fprintf(stderr, "%s: read error\n", progname);
00164                      exit(1);
00165               }
00166               if (fhoriz)
00167                      for (x = 0; x < xres; x++)
00168                             copycolr(scanout[x], scanin[xres-1-x]);
00169               if (fwritecolrs(scanout, xres, stdout) < 0) {
00170                      fprintf(stderr, "%s: write error\n", progname);
00171                      exit(1);
00172               }
00173        }
00174        free((void *)scanin);
00175        if (fhoriz)
00176               free((void *)scanout);
00177 }