--- ray/src/util/rpiece.c 2003/07/21 22:30:19 2.40 +++ ray/src/util/rpiece.c 2013/12/05 03:02:56 2.56 @@ -1,17 +1,32 @@ #ifndef lint -static const char RCSid[] = "$Id: rpiece.c,v 2.40 2003/07/21 22:30:19 schorsch Exp $"; +static const char RCSid[] = "$Id: rpiece.c,v 2.56 2013/12/05 03:02:56 greg Exp $"; #endif /* * Generate sections of a picture. */ + +#include +#include +#include + +#include "platform.h" +#ifndef NON_POSIX /* XXX need abstraction for process management */ + #include +#endif + #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); @@ -19,16 +34,14 @@ char *argv[]; #else -#include - -#include "color.h" -#include "view.h" -#include "rtprocess.h" - #ifndef NFS #define NFS 1 #endif - /* set the following to 0 to forgo forking */ + +#ifndef RHAS_FORK_EXEC +#undef MAXFORK +#define MAXFORK 0 +#endif #ifndef MAXFORK #if NFS #define MAXFORK 3 /* allotment of duped processes */ @@ -42,8 +55,8 @@ 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(); @@ -69,18 +82,30 @@ int nforked = 0; char *progname; int verbose = 0; +int nowarn = 0; unsigned timelim = 0; int rvrlim = -1; int gotalrm = 0; 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; + int i, rval; progname = argv[0]; for (i = 1; i < argc; i++) { @@ -88,12 +113,20 @@ char *argv[]; while ((rval = expandarg(&argc, &argv, i)) > 0) ; if (rval < 0) { - fprintf(stderr, "%s: cannot expand '%s'", + fprintf(stderr, "%s: cannot expand '%s'\n", argv[0], argv[i]); exit(1); } if (argv[i][0] == '-') switch (argv[i][1]) { + case 'w': + if (!argv[i][2]) + nowarn = !nowarn; + else if (argv[i][2] == '+') + nowarn = 0; + else if (argv[i][2] == '-') + nowarn = 1; + break; case 'v': switch (argv[i][2]) { case '\0': /* verbose option */ @@ -116,6 +149,12 @@ char *argv[]; } break; case 'p': /* pixel aspect ratio? */ + if (argv[i][2] == 'm') { + fprintf(stderr, "%s: -pm unsupported\n", + argv[0]); + ++i; + continue; + } if (argv[i][2] != 'a' || argv[i][3]) break; pixaspect = atof(argv[++i]); @@ -124,7 +163,7 @@ char *argv[]; if (argv[i][2]) break; timelim = atof(argv[++i])*3600. + .5; - break; + continue; case 'x': /* overall x resolution */ if (argv[i][2]) break; @@ -169,8 +208,9 @@ char *argv[]; break; outfile = argv[++i]; continue; - } else if (i >= argc-1) - break; + } + else if (i >= argc-1) + break; rpargv[rpargc++] = argv[i]; } if (i >= argc) { @@ -187,9 +227,11 @@ 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 */ @@ -197,14 +239,16 @@ int ltyp; if (fcntl(fd, F_SETLKW, &fls) < 0) { fprintf(stderr, "%s: cannot lock/unlock file: %s\n", progname, strerror(errno)); - exit(1); + _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[]; @@ -223,9 +267,20 @@ char **av; sflock(F_UNLCK); } /* compute piece size */ + hr = hres; vr = vres; + if (pixaspect > FTINY) + normaspect(viewaspect(&ourview), &pixaspect, &hr, &vr); hres /= hmult; vres /= vmult; + if (hres <= 0 || vres <= 0) { + fprintf(stderr, "%s: illegal resolution/subdivision\n", progname); + exit(1); + } normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); + if (!nowarn && (hr != hres*hmult) | (vr != vres*vmult)) + fprintf(stderr, + "%s: warning - resolution changed from %dx%d to %dx%d\n", + progname, hr, vr, hres*hmult, vres*vmult); sprintf(hrbuf, "%d", hres); rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; sprintf(vrbuf, "%d", vres); @@ -244,6 +299,7 @@ char **av; fputs(VIEWSTR, fp); fprintview(&ourview, fp); putc('\n', fp); + fputnow(fp); if (pixaspect < .99 || pixaspect > 1.01) fputaspect(pixaspect, fp); fputformat(COLRFMT, fp); @@ -294,9 +350,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); @@ -336,13 +394,15 @@ int *xp, *yp; } -int -rvrpiece(xp, yp) /* check for recoverable pieces */ -register int *xp, *yp; +static int +rvrpiece( /* check for recoverable pieces */ + int *xp, + int *yp +) { static char *pdone = NULL; /* which pieces are done */ static long readpos = -1; /* how far we've read */ - register int i; + int px, py, i; /* * This routine is called by nextpiece() with an * exclusive lock on syncfp and the file pointer at the @@ -351,15 +411,15 @@ register int *xp, *yp; if (rvrlim < 0) return(0); /* only check if asked */ if (pdone == NULL) /* first call */ - pdone = calloc(hmult*vmult, sizeof(char)); + pdone = (char *)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) - pdone[*xp*vmult+*yp] = 1; + while (fscanf(syncfp, "%d %d", &px, &py) == 2) + pdone[px*vmult+py] = 1; if (!feof(syncfp)) { fprintf(stderr, "%s: format error in sync file\n", progname); exit(1); @@ -380,9 +440,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; @@ -396,7 +457,8 @@ int rstat; } -rpiece() /* render picture piece by piece */ +static void +rpiece(void) /* render picture piece by piece */ { VIEW pview; int xorg, yorg; @@ -404,10 +466,10 @@ rpiece() /* render picture piece by piece */ pview = ourview; switch (ourview.type) { case VT_PER: - pview.horiz = 2.*180./PI*atan( - tan(PI/180./2.*ourview.horiz)/hmult ); - pview.vert = 2.*180./PI*atan( - tan(PI/180./2.*ourview.vert)/vmult ); + pview.horiz = (2.*180./PI)*atan( + tan((PI/180./2.)*ourview.horiz)/hmult ); + pview.vert = (2.*180./PI)*atan( + tan((PI/180./2.)*ourview.vert)/vmult ); break; case VT_PAR: case VT_ANG: @@ -416,15 +478,27 @@ rpiece() /* render picture piece by piece */ break; case VT_CYL: pview.horiz = ourview.horiz / hmult; - pview.vert = 2.*180./PI*atan( - tan(PI/180./2.*ourview.vert)/vmult ); + 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 ); - pview.vert = 2.*180./PI*asin( - sin(PI/180./2.*ourview.vert)/vmult ); + pview.horiz = (2.*180./PI)*asin( + sin((PI/180./2.)*ourview.horiz)/hmult ); + pview.vert = (2.*180./PI)*asin( + sin((PI/180./2.)*ourview.vert)/vmult ); break; + case VT_PLS: + pview.horiz = sin((PI/180./2.)*ourview.horiz) / + (1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult; + pview.horiz *= pview.horiz; + pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) / + (1. + pview.horiz)); + pview.vert = sin((PI/180./2.)*ourview.vert) / + (1.0 + cos((PI/180./2.)*ourview.vert)) / vmult; + pview.vert *= pview.vert; + pview.vert = (2.*180./PI)*acos((1. - pview.vert) / + (1. + pview.vert)); + break; default: fprintf(stderr, "%s: unknown view type '-vt%c'\n", progname, ourview.type); @@ -443,16 +517,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; + 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)); @@ -460,7 +536,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)); @@ -501,11 +577,15 @@ int xpos, ypos; /* lock file section so NFS doesn't mess up */ fls.l_whence = 0; fls.l_type = F_WRLCK; +#if 0 if (fcntl(outfd, F_SETLKW, &fls) < 0) filerr("lock"); +#else + dolock(outfd, F_WRLCK); #endif +#endif /* write new piece to file */ - if (lseek(outfd, (off_t)fls.l_start, 0) < 0) + if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) filerr("seek"); if (hmult == 1) { if (writebuf(outfd, (char *)pbuf, @@ -518,14 +598,18 @@ int xpos, ypos; filerr("write"); if (y < vr-1 && lseek(outfd, (off_t)(hmult-1)*hr*sizeof(COLR), - 1) < 0) + SEEK_CUR) < 0) filerr("seek"); } #if NFS fls.l_type = F_UNLCK; /* release lock */ +#if 0 if (fcntl(outfd, F_SETLKW, &fls) < 0) filerr("lock"); +#else + dolock(outfd, F_UNLCK); #endif +#endif if (verbose) { /* notify caller */ printf("%d %d done\n", xpos, ypos); fflush(stdout); @@ -544,8 +628,10 @@ int xpos, ypos; } -filerr(t) /* report file error and exit */ -char *t; +static void +filerr( /* report file error and exit */ + char *t +) { fprintf(stderr, "%s: %s error on file \"%s\": %s\n", progname, t, outfile, strerror(errno));