--- ray/src/util/rpiece.c 1993/10/19 16:02:39 2.28 +++ ray/src/util/rpiece.c 2004/06/08 19:48:31 2.44 @@ -1,20 +1,29 @@ -/* Copyright (c) 1993 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rpiece.c,v 2.44 2004/06/08 19:48:31 greg Exp $"; #endif - /* * Generate sections of a picture. */ + +#include +#include +#include +#include + +#include "platform.h" #include "standard.h" +#include "color.h" +#include "view.h" +#include "rtprocess.h" #ifndef F_SETLKW -main(argc, argv) -int argc; -char *argv[]; +int +main( + int argc, + char *argv[] +) { fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); exit(1); @@ -22,11 +31,6 @@ char *argv[]; #else -#include -#include "color.h" -#include "view.h" -#include "resolu.h" - #ifndef NFS #define NFS 1 #endif @@ -44,13 +48,15 @@ char *argv[]; #define unguard() sigrelse(SIGALRM) #endif #ifndef guard_io -#define guard_io() 0 -#define unguard() 0 +#define guard_io() +#define unguard() #endif + +extern char *strerror(); + /* rpict command */ char *rpargv[128] = {"rpict", "-S", "1"}; int rpargc = 3; -int rpd[3]; FILE *torp, *fromrp; COLR *pbuf; /* our view parameters */ @@ -69,29 +75,40 @@ int nforked = 0; char *progname; int verbose = 0; +unsigned timelim = 0; int rvrlim = -1; -extern long lseek(), ftell(); - int gotalrm = 0; -int onalrm() { gotalrm++; } +void onalrm(int i) { gotalrm++; } +static void dolock(int fd, int ltyp); +static void init(int ac, char **av); +static int nextpiece(int *xp, int *yp); +static int rvrpiece(int *xp, int *yp); +static int cleanup(int rstat); +static void rpiece(void); +static int putpiece(int xpos, int ypos); +static void filerr(char *t); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { register int i, rval; progname = argv[0]; for (i = 1; i < argc; i++) { /* expand arguments */ - while (rval = expandarg(&argc, &argv, i)) - if (rval < 0) { - fprintf(stderr, "%s: cannot expand '%s'", - argv[0], argv[i]); - exit(1); - } + while ((rval = expandarg(&argc, &argv, i)) > 0) + ; + if (rval < 0) { + fprintf(stderr, "%s: cannot expand '%s'", + argv[0], argv[i]); + exit(1); + } if (argv[i][0] == '-') switch (argv[i][1]) { case 'v': @@ -118,8 +135,13 @@ char *argv[]; case 'p': /* pixel aspect ratio? */ if (argv[i][2] != 'a' || argv[i][3]) break; - pixaspect = atof(argv[i+1]); + pixaspect = atof(argv[++i]); continue; + case 'T': /* time limit (hours) */ + if (argv[i][2]) + break; + timelim = atof(argv[++i])*3600. + .5; + break; case 'x': /* overall x resolution */ if (argv[i][2]) break; @@ -182,31 +204,35 @@ char *argv[]; } -dolock(fd, ltyp) /* lock or unlock a file */ -int fd; -int ltyp; +static void +dolock( /* lock or unlock a file */ + int fd, + int ltyp +) { static struct flock fls; /* static so initialized to zeroes */ - extern char *sys_errlist[]; fls.l_type = ltyp; if (fcntl(fd, F_SETLKW, &fls) < 0) { fprintf(stderr, "%s: cannot lock/unlock file: %s\n", - progname, sys_errlist[errno]); + progname, strerror(errno)); exit(1); } } -init(ac, av) /* set up output file and start rpict */ -int ac; -char **av; +static void +init( /* set up output file and start rpict */ + int ac, + char **av +) { static char hrbuf[16], vrbuf[16]; extern char VersionID[]; char *err; FILE *fp; int hr, vr; + SUBPROC rpd; /* since we don't close_process(), this can be local */ /* set up view */ if ((err = setview(&ourview)) != NULL) { fprintf(stderr, "%s: %s\n", progname, err); @@ -233,7 +259,8 @@ char **av; dolock(outfd, F_WRLCK); if ((fp = fdopen(dup(outfd), "w")) == NULL) goto filerr; - printargs(ac, av, fp); /* write header */ + newheader("RADIANCE", fp); /* create header */ + printargs(ac, av, fp); fprintf(fp, "SOFTWARE= %s\n", VersionID); fputs(VIEWSTR, fp); fprintview(&ourview, fp); @@ -264,12 +291,12 @@ char **av; goto filerr; dolock(outfd, F_UNLCK); /* start rpict process */ - if (open_process(rpd, rpargv) <= 0) { + if (open_process(&rpd, rpargv) <= 0) { fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); exit(1); } - if ((fromrp = fdopen(rpd[0], "r")) == NULL || - (torp = fdopen(rpd[1], "w")) == NULL) { + if ((fromrp = fdopen(rpd.r, "r")) == NULL || + (torp = fdopen(rpd.w, "w")) == NULL) { fprintf(stderr, "%s: cannot open stream to %s\n", progname, rpargv[0]); exit(1); @@ -279,6 +306,8 @@ char **av; exit(1); } signal(SIGALRM, onalrm); + if (timelim) + alarm(timelim); return; filerr: fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); @@ -286,9 +315,11 @@ filerr: } -int -nextpiece(xp, yp) /* get next piece assignment */ -int *xp, *yp; +static int +nextpiece( /* get next piece assignment */ + int *xp, + int *yp +) { if (gotalrm) /* someone wants us to quit */ return(0); @@ -328,9 +359,11 @@ int *xp, *yp; } -int -rvrpiece(xp, yp) /* check for recoverable pieces */ -register int *xp, *yp; +static int +rvrpiece( /* check for recoverable pieces */ + register int *xp, + register int *yp +) { static char *pdone = NULL; /* which pieces are done */ static long readpos = -1; /* how far we've read */ @@ -344,6 +377,10 @@ register int *xp, *yp; return(0); /* only check if asked */ if (pdone == NULL) /* first call */ pdone = calloc(hmult*vmult, sizeof(char)); + if (pdone == NULL) { + fprintf(stderr, "%s: out of memory\n", progname); + exit(1); + } if (readpos != -1) /* mark what's been done */ fseek(syncfp, readpos, 0); while (fscanf(syncfp, "%d %d", xp, yp) == 2) @@ -368,9 +405,10 @@ register int *xp, *yp; } -int -cleanup(rstat) /* close rpict process and clean up */ -int rstat; +static int +cleanup( /* close rpict process and clean up */ + int rstat +) { int status; @@ -384,12 +422,13 @@ int rstat; } -rpiece() /* render picture piece by piece */ +static void +rpiece(void) /* render picture piece by piece */ { VIEW pview; int xorg, yorg; /* compute view parameters */ - copystruct(&pview, &ourview); + pview = ourview; switch (ourview.type) { case VT_PER: pview.horiz = 2.*180./PI*atan( @@ -402,6 +441,11 @@ rpiece() /* render picture piece by piece */ pview.horiz = ourview.horiz / hmult; pview.vert = ourview.vert / vmult; break; + case VT_CYL: + pview.horiz = ourview.horiz / hmult; + pview.vert = 2.*180./PI*atan( + tan(PI/180./2.*ourview.vert)/vmult ); + break; case VT_HEM: pview.horiz = 2.*180./PI*asin( sin(PI/180./2.*ourview.horiz)/hmult ); @@ -415,8 +459,8 @@ rpiece() /* render picture piece by piece */ } /* render each piece */ while (nextpiece(&xorg, &yorg)) { - pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); - pview.voff = ourview.voff + yorg - 0.5*(vmult-1); + pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); + pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); fputs(VIEWSTR, torp); fprintview(&pview, torp); putc('\n', torp); @@ -426,16 +470,18 @@ rpiece() /* render picture piece by piece */ } -int -putpiece(xpos, ypos) /* get next piece from rpict */ -int xpos, ypos; +static int +putpiece( /* get next piece from rpict */ +int xpos, +int ypos +) { struct flock fls; int pid, status; int hr, vr; register int y; /* check bounds */ - if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { + if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", progname, xpos, ypos); exit(cleanup(1)); @@ -443,7 +489,7 @@ int xpos, ypos; /* check header from rpict */ guard_io(); getheader(fromrp, NULL, NULL); - if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { + if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) { fprintf(stderr, "%s: resolution mismatch from %s\n", progname, rpargv[0]); exit(cleanup(1)); @@ -488,7 +534,7 @@ int xpos, ypos; filerr("lock"); #endif /* write new piece to file */ - if (lseek(outfd, fls.l_start, 0) == -1) + if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) filerr("seek"); if (hmult == 1) { if (writebuf(outfd, (char *)pbuf, @@ -500,8 +546,8 @@ int xpos, ypos; hr*sizeof(COLR)) != hr*sizeof(COLR)) filerr("write"); if (y < vr-1 && lseek(outfd, - (long)(hmult-1)*hr*sizeof(COLR), - 1) == -1) + (off_t)(hmult-1)*hr*sizeof(COLR), + SEEK_CUR) < 0) filerr("seek"); } #if NFS @@ -509,6 +555,10 @@ int xpos, ypos; if (fcntl(outfd, F_SETLKW, &fls) < 0) filerr("lock"); #endif + if (verbose) { /* notify caller */ + printf("%d %d done\n", xpos, ypos); + fflush(stdout); + } if (syncfp != NULL) { /* record what's been done */ sflock(F_WRLCK); fseek(syncfp, 0L, 2); /* append index */ @@ -517,23 +567,19 @@ int xpos, ypos; /*** Unlock not necessary, since sflock(F_UNLCK); _exit() or nextpiece() is next ***/ } - if (verbose) { /* notify caller */ - printf("%d %d done\n", xpos, ypos); - fflush(stdout); - } if (pid == -1) /* didn't fork or fork failed */ return(0); _exit(0); /* else exit child process (releasing locks) */ } -filerr(t) /* report file error and exit */ -char *t; +static void +filerr( /* report file error and exit */ + char *t +) { - extern char *sys_errlist[]; - fprintf(stderr, "%s: %s error on file \"%s\": %s\n", - progname, t, outfile, sys_errlist[errno]); + progname, t, outfile, strerror(errno)); _exit(1); }