--- ray/src/px/pflip.c 1991/01/09 11:15:13 1.1 +++ ray/src/px/pflip.c 2004/03/28 20:33:14 2.8 @@ -1,34 +1,64 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pflip.c,v 2.8 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * flip picture file horizontally and/or vertically */ -#include "standard.h" +#include +#include +#include +#include "platform.h" #include "color.h" +#include "resolu.h" -int xres, yres; /* input resolution */ +int order; /* input orientation */ +int xres, yres; /* resolution (scanlen, nscans) */ long *scanpos; /* scanline positions */ -int fhoriz, fvert; /* flip flags */ +int fhoriz=0, fvert=0; /* flip flags */ +int correctorder = 0; /* correcting orientation? */ + FILE *fin; /* input file */ char *progname; -main(argc, argv) -int argc; -char *argv[]; +static void memerr(void); +static void scanfile(void); +static void flip(void); + + +static int +neworder(void) /* figure out new order from old */ { - int i; + register int no; + if (correctorder) + return(order); /* just leave it */ + if ((no = order) & YMAJOR) { + if (fhoriz) no ^= XDECR; + if (fvert) no ^= YDECR; + } else { + if (fhoriz) no ^= YDECR; + if (fvert) no ^= XDECR; + } + return(no); +} + +int +main( + int argc, + char *argv[] +) +{ + static char picfmt[LPICFMT+1] = PICFMT; + int i, rval; + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdout); progname = argv[0]; for (i = 1; i < argc; i++) @@ -36,10 +66,12 @@ char *argv[]; fhoriz++; else if (!strcmp(argv[i], "-v")) fvert++; + else if (!strcmp(argv[i], "-c")) + correctorder++; else break; if (i >= argc || argv[i][0] == '-') { - fprintf(stderr, "Usage: %s [-h][-v] infile [outfile]\n", + fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n", progname); exit(1); } @@ -52,17 +84,23 @@ char *argv[]; exit(1); } /* transfer header */ - copyheader(fin, stdout); + if ((rval = checkheader(fin, picfmt, stdout)) < 0) { + fprintf(stderr, "%s: input not a Radiance picture\n", + progname); + exit(1); + } + if (rval) + fputformat(picfmt, stdout); /* add new header info. */ - printargs(argc, argv, stdout); + printargs(i, argv, stdout); putchar('\n'); /* get picture size */ - if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { + if ((order = fgetresolu(&xres, &yres, fin)) < 0) { fprintf(stderr, "%s: bad picture size\n", progname); exit(1); } /* write new picture size */ - fputresolu(YMAJOR|YDECR, xres, yres, stdout); + fputresolu(neworder(), xres, yres, stdout); /* goto end if vertical flip */ if (fvert) scanfile(); @@ -71,14 +109,16 @@ char *argv[]; } -memerr() +static void +memerr(void) { fprintf(stderr, "%s: out of memory\n", progname); exit(1); } -scanfile() /* scan to the end of file */ +static void +scanfile(void) /* scan to the end of file */ { extern long ftell(); COLR *scanin; @@ -88,18 +128,20 @@ scanfile() /* scan to the end of file */ memerr(); if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) memerr(); - for (y = yres-1; y >= 0; y--) { + for (y = yres-1; y > 0; y--) { scanpos[y] = ftell(fin); if (freadcolrs(scanin, xres, fin) < 0) { fprintf(stderr, "%s: read error\n", progname); exit(1); } } - free((char *)scanin); + scanpos[0] = ftell(fin); + free((void *)scanin); } -flip() /* flip the picture */ +static void +flip(void) /* flip the picture */ { COLR *scanin, *scanout; int y; @@ -129,6 +171,7 @@ flip() /* flip the picture */ exit(1); } } - free((char *)scanin); - free((char *)scanout); + free((void *)scanin); + if (fhoriz) + free((void *)scanout); }