--- ray/src/px/protate.c 1990/12/04 11:25:46 1.7 +++ ray/src/px/protate.c 1998/10/27 16:32:33 2.6 @@ -1,8 +1,9 @@ -/* Copyright (c) 1988 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif + /* * prot.c - program to rotate picture file 90 degrees clockwise. * @@ -13,10 +14,19 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" +#include "resolu.h" + +int order; /* input scanline order */ int xres, yres; /* input resolution */ -double inpaspect = 1.0; /* input aspect ratio */ +int correctorder = 0; /* order correction? */ +int ccw = 0; /* rotate CCW? */ + +#ifdef BIGMEM +char buf[1<<22]; /* output buffer */ +#else char buf[1<<20]; /* output buffer */ +#endif int nrows; /* number of rows output at once */ @@ -24,13 +34,23 @@ int nrows; /* number of rows output at once */ char *progname; +short ordertab[4][2] = { + {0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0} +}; -headline(s) /* process line from header */ -char *s; + +int +neworder() /* return corrected order */ { - fputs(s, stdout); - if (isaspect(s)) - inpaspect *= aspectval(s); + register int i; + + if (correctorder) + return(order); + for (i = 4; i--; ) + if ((order&~YMAJOR) == ordertab[i][ccw]) + return(ordertab[i][1-ccw] | ((order&YMAJOR)^YMAJOR)); + fputs("Order botch!\n", stderr); + exit(2); } @@ -38,14 +58,27 @@ main(argc, argv) int argc; char *argv[]; { + static char picfmt[LPICFMT+1] = PICFMT; + int rval; FILE *fin; progname = argv[0]; - if (argc != 2 && argc != 3) { - fprintf(stderr, "Usage: %s infile [outfile]\n", progname); - exit(1); + while (argc > 2 && argv[1][0] == '-') { + switch (argv[1][1]) { + case 'c': + correctorder = 1; + break; + case 'r': + ccw = 1; + break; + default: + goto userr; + } + argc--; argv++; } + if (argc != 2 && argc != 3) + goto userr; if ((fin = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: cannot open\n", argv[1]); exit(1); @@ -55,26 +88,38 @@ char *argv[]; exit(1); } /* transfer header */ - getheader(fin, headline); + if ((rval = checkheader(fin, picfmt, stdout)) < 0) { + fprintf(stderr, "%s: not a Radiance picture\n", progname); + exit(1); + } + if (rval) + fputformat(picfmt, stdout); /* add new header info. */ - if (inpaspect < .99 || inpaspect > 1.01) - fputaspect(1./inpaspect/inpaspect, stdout); - printf("%s\n\n", progname); + fputs(progname, stdout); + if (ccw) fputs(" -r", stdout); + if (correctorder) fputs(" -c", stdout); + fputs("\n\n", stdout); /* 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, yres, xres, stdout); + fputresolu(neworder(), yres, xres, stdout); /* compute buffer capacity */ nrows = sizeof(buf)/sizeof(COLR)/yres; - rotate(fin); /* rotate the image */ + if (ccw) /* rotate the image */ + rotateccw(fin); + else + rotatecw(fin); exit(0); +userr: + fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); + exit(1); } -rotate(fp) /* rotate picture */ +rotatecw(fp) /* rotate picture clockwise */ FILE *fp; { register COLR *inln; @@ -97,11 +142,46 @@ FILE *fp; exit(1); } for (inx = 0; inx < nrows && xoff+inx < xres; inx++) - bcopy((char *)inln[xoff+inx], - (char *)scanbar[inx*yres+iny], - sizeof(COLR)); + copycolr(scanbar[inx*yres+iny], + inln[xoff+inx]); } for (inx = 0; inx < nrows && xoff+inx < xres; inx++) + if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { + fprintf(stderr, "%s: write error\n", progname); + exit(1); + } + } + free((char *)inln); +} + + +rotateccw(fp) /* rotate picture counter-clockwise */ +FILE *fp; +{ + register COLR *inln; + register int xoff, inx, iny; + long start, ftell(); + + if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { + fprintf(stderr, "%s: out of memory\n", progname); + exit(1); + } + start = ftell(fp); + for (xoff = xres-1; xoff >= 0; xoff -= nrows) { + if (fseek(fp, start, 0) < 0) { + fprintf(stderr, "%s: seek error\n", progname); + exit(1); + } + for (iny = 0; iny < yres; iny++) { + if (freadcolrs(inln, xres, fp) < 0) { + fprintf(stderr, "%s: read error\n", progname); + exit(1); + } + for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) + copycolr(scanbar[inx*yres+iny], + inln[xoff-inx]); + } + for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); exit(1);