--- ray/src/px/pinterp.c 1991/04/18 14:35:26 1.33 +++ ray/src/px/pinterp.c 1994/12/21 16:59:03 2.14 @@ -1,3 +1,5 @@ +/* Copyright (c) 1994 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif @@ -10,15 +12,13 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" -#include +#include #include "view.h" #include "color.h" -#ifndef BSD -#define vfork fork -#endif +#include "resolu.h" #define pscan(y) (ourpict+(y)*hresolu) #define zscan(y) (ourzbuf+(y)*hresolu) @@ -26,9 +26,9 @@ static char SCCSid[] = "$SunId$ LBL"; #define F_FORE 1 /* fill foreground */ #define F_BACK 2 /* fill background */ -#define PACKSIZ 42 /* calculation packet size */ +#define PACKSIZ 256 /* max. calculation packet size */ -#define RTCOM "rtrace -h -ovl -fff %s" +#define RTCOM "rtrace -h- -ovl -fff " #define ABS(x) ((x)>0?(x):-(x)) @@ -58,14 +58,15 @@ double ourexp = -1; /* output picture exposure */ VIEW theirview = STDVIEW; /* input view */ int gotview; /* got input view? */ int wrongformat = 0; /* input in another format? */ -int thresolu, tvresolu; /* input resolution */ +RESOLU tresolu; /* input resolution */ double theirexp; /* input picture exposure */ double theirs2ours[4][4]; /* transformation matrix */ int hasmatrix = 0; /* has transformation matrix */ -int childpid = -1; /* id of fill process */ -FILE *psend, *precv; /* pipes to/from fill calculation */ -int queue[PACKSIZ][2]; /* pending pixels */ +int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */ +#define childpid (PDesc[2]) +unsigned short queue[PACKSIZ][2]; /* pending pixels */ +int packsiz; /* actual packet size */ int queuesiz; /* number of pixels pending */ @@ -73,8 +74,9 @@ main(argc, argv) /* interpolate pictures */ int argc; char *argv[]; { -#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt - extern double atof(); +#define check(ol,al) if (argv[i][ol] || \ + badarg(argc-i-1,argv+i+1,al)) \ + goto badopt int gotvfile = 0; char *zfile = NULL; char *err; @@ -90,49 +92,49 @@ char *argv[]; } switch (argv[i][1]) { case 't': /* threshold */ - check(2,1); + check(2,"f"); zeps = atof(argv[++i]); break; case 'n': /* dist. normalized? */ - check(2,0); + check(2,NULL); normdist = !normdist; break; case 'f': /* fill type */ switch (argv[i][2]) { case '0': /* none */ - check(3,0); + check(3,NULL); fillo = 0; break; case 'f': /* foreground */ - check(3,0); + check(3,NULL); fillo = F_FORE; break; case 'b': /* background */ - check(3,0); + check(3,NULL); fillo = F_BACK; break; case 'a': /* all */ - check(3,0); + check(3,NULL); fillo = F_FORE|F_BACK; break; case 's': /* sample */ - check(3,1); + check(3,"i"); fillsamp = atoi(argv[++i]); break; case 'c': /* color */ - check(3,3); + check(3,"fff"); fillfunc = backfill; setcolr(backcolr, atof(argv[i+1]), atof(argv[i+2]), atof(argv[i+3])); i += 3; break; case 'z': /* z value */ - check(3,1); + check(3,"f"); fillfunc = backfill; backz = atof(argv[++i]); break; case 'r': /* rtrace */ - check(3,1); + check(3,"s"); fillfunc = rcalfill; calstart(RTCOM, argv[++i]); break; @@ -141,30 +143,31 @@ char *argv[]; } break; case 'z': /* z file */ - check(2,1); + check(2,"s"); zfile = argv[++i]; break; case 'x': /* x resolution */ - check(2,1); + check(2,"i"); hresolu = atoi(argv[++i]); break; case 'y': /* y resolution */ - check(2,1); + check(2,"i"); vresolu = atoi(argv[++i]); break; case 'p': /* pixel aspect */ - check(2,1); + if (argv[i][2] != 'a') + goto badopt; + check(3,"f"); pixaspect = atof(argv[++i]); break; case 'v': /* view file */ if (argv[i][2] != 'f') goto badopt; - check(3,1); + check(3,"s"); gotvfile = viewfile(argv[++i], &ourview, 0, 0); - if (gotvfile < 0) { - perror(argv[i]); - exit(1); - } else if (gotvfile == 0) { + if (gotvfile < 0) + syserror(argv[i]); + else if (gotvfile == 0) { fprintf(stderr, "%s: bad view file\n", argv[i]); exit(1); @@ -180,17 +183,26 @@ char *argv[]; /* check arguments */ if ((argc-i)%2) goto userr; + if (fillsamp == 1) + fillo &= ~F_BACK; /* set view */ - if (err = setview(&ourview)) { + if (ourview.vaft > FTINY) + err = "no aft clipping plane allowed"; + else + err = setview(&ourview); + if (err != NULL) { fprintf(stderr, "%s: %s\n", progname, err); exit(1); } normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); /* allocate frame */ - ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR)); - ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); + ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); + ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float)); if (ourpict == NULL || ourzbuf == NULL) - syserror(); + syserror(progname); + bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); + /* new header */ + newheader("RADIANCE", stdout); /* get input */ for ( ; i < argc; i += 2) addpicture(argv[i], argv[i+1]); @@ -233,12 +245,11 @@ userr: headline(s) /* process header string */ char *s; { - static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; - register char **an; char fmt[32]; - if (isformat(s)) { - formatval(fmt, s); + if (isheadid(s)) + return; + if (formatval(fmt, s)) { wrongformat = strcmp(fmt, COLRFMT); return; } @@ -249,19 +260,14 @@ char *s; theirexp *= exposval(s); return; } - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&theirview, s+strlen(*an)) > 0) - gotview++; - break; - } + if (isview(s) && sscanview(&theirview, s) > 0) + gotview++; } addpicture(pfile, zspec) /* add picture to output */ char *pfile, *zspec; { - extern double atof(); FILE *pfp; int zfd; char *err; @@ -270,18 +276,14 @@ char *pfile, *zspec; struct position *plast; int y; /* open picture file */ - if ((pfp = fopen(pfile, "r")) == NULL) { - perror(pfile); - exit(1); - } + if ((pfp = fopen(pfile, "r")) == NULL) + syserror(pfile); /* get header with exposure and view */ theirexp = 1.0; gotview = 0; printf("%s:\n", pfile); getheader(pfp, headline, NULL); - if (wrongformat || !gotview || - fgetresolu(&thresolu, &tvresolu, pfp) != (YMAJOR|YDECR)) { - + if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) { fprintf(stderr, "%s: picture format error\n", pfile); exit(1); } @@ -296,30 +298,30 @@ char *pfile, *zspec; /* compute transformation */ hasmatrix = pixform(theirs2ours, &theirview, &ourview); /* allocate scanlines */ - scanin = (COLR *)malloc(thresolu*sizeof(COLR)); - zin = (float *)malloc(thresolu*sizeof(float)); - plast = (struct position *)calloc(thresolu, sizeof(struct position)); + scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); + zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); + plast = (struct position *)calloc(scanlen(&tresolu), + sizeof(struct position)); if (scanin == NULL || zin == NULL || plast == NULL) - syserror(); + syserror(progname); /* get z specification or file */ if ((zfd = open(zspec, O_RDONLY)) == -1) { double zvalue; register int x; - if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { - perror(zspec); - exit(1); - } - for (x = 0; x < thresolu; x++) + if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) + syserror(zspec); + for (x = scanlen(&tresolu); x-- > 0; ) zin[x] = zvalue; } /* load image */ - for (y = tvresolu-1; y >= 0; y--) { - if (freadcolrs(scanin, thresolu, pfp) < 0) { + for (y = 0; y < numscans(&tresolu); y++) { + if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { fprintf(stderr, "%s: read error\n", pfile); exit(1); } - if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float)) - < thresolu*sizeof(float)) { + if (zfd != -1 && read(zfd,(char *)zin, + scanlen(&tresolu)*sizeof(float)) + < scanlen(&tresolu)*sizeof(float)) { fprintf(stderr, "%s: read error\n", zspec); exit(1); } @@ -382,15 +384,13 @@ COLR *pline; float *zline; struct position *lasty; /* input/output */ { - extern double sqrt(); FVECT pos; struct position lastx, newpos; register int x; lastx.z = 0; - for (x = thresolu-1; x >= 0; x--) { - pos[0] = (x+.5)/thresolu; - pos[1] = (y+.5)/tvresolu; + for (x = scanlen(&tresolu); x-- > 0; ) { + pix2loc(pos, &tresolu, x, y); pos[2] = zline[x]; if (movepixel(pos) < 0) { lasty[x].z = lastx.z = 0; /* mark invalid */ @@ -498,12 +498,12 @@ FVECT pos; pos[1] += .5 - ourview.voff; return(0); } - if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) + if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY) return(-1); pt[0] += direc[0]*pos[2]; pt[1] += direc[1]*pos[2]; pt[2] += direc[2]*pos[2]; - viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt); + viewloc(pos, &ourview, pt); if (pos[2] <= 0) return(-1); return(0); @@ -520,7 +520,7 @@ int samp; /* get back buffer */ yback = (int *)malloc(hresolu*sizeof(int)); if (yback == NULL) - syserror(); + syserror(progname); for (x = 0; x < hresolu; x++) yback[x] = -2; /* @@ -621,29 +621,26 @@ writepicture() /* write out picture */ { int y; - fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); + fprtresolu(hresolu, vresolu, stdout); for (y = vresolu-1; y >= 0; y--) if (fwritecolrs(pscan(y), hresolu, stdout) < 0) - syserror(); + syserror(progname); } writedistance(fname) /* write out z file */ char *fname; { - extern double sqrt(); int donorm = normdist && ourview.type == VT_PER; int fd; int y; float *zout; - if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { - perror(fname); - exit(1); - } + if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) + syserror(fname); if (donorm && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) - syserror(); + syserror(progname); for (y = vresolu-1; y >= 0; y--) { if (donorm) { double vx, yzn2; @@ -658,10 +655,8 @@ char *fname; } else zout = zscan(y); if (write(fd, (char *)zout, hresolu*sizeof(float)) - < hresolu*sizeof(float)) { - perror(fname); - exit(1); - } + < hresolu*sizeof(float)) + syserror(fname); } if (donorm) free((char *)zout); @@ -690,57 +685,52 @@ int x, y; } -calstart(prog, args) /* start fill calculation */ +calstart(prog, args) /* start fill calculation */ char *prog, *args; { char combuf[512]; - int p0[2], p1[2]; + char *argv[64]; + int rval; + register char **wp, *cp; if (childpid != -1) { fprintf(stderr, "%s: too many calculations\n", progname); exit(1); } - sprintf(combuf, prog, args); - if (pipe(p0) < 0 || pipe(p1) < 0) - syserror(); - if ((childpid = vfork()) == 0) { /* fork calculation */ - close(p0[1]); - close(p1[0]); - if (p0[0] != 0) { - dup2(p0[0], 0); - close(p0[0]); - } - if (p1[1] != 1) { - dup2(p1[1], 1); - close(p1[1]); - } - execl("/bin/sh", "sh", "-c", combuf, 0); - perror("/bin/sh"); - _exit(127); + strcpy(combuf, prog); + strcat(combuf, args); + cp = combuf; + wp = argv; + for ( ; ; ) { + while (isspace(*cp)) /* nullify spaces */ + *cp++ = '\0'; + if (!*cp) /* all done? */ + break; + *wp++ = cp; /* add argument to list */ + while (*++cp && !isspace(*cp)) + ; } - if (childpid == -1) - syserror(); - close(p0[0]); - close(p1[1]); - if ((psend = fdopen(p0[1], "w")) == NULL) - syserror(); - if ((precv = fdopen(p1[0], "r")) == NULL) - syserror(); + *wp = NULL; + /* start process */ + if ((rval = open_process(PDesc, argv)) < 0) + syserror(progname); + if (rval == 0) { + fprintf(stderr, "%s: command not found\n", argv[0]); + exit(1); + } + packsiz = rval/(6*sizeof(float)) - 1; + if (packsiz > PACKSIZ) + packsiz = PACKSIZ; queuesiz = 0; } -caldone() /* done with calculation */ +caldone() /* done with calculation */ { - int pid; - if (childpid == -1) return; clearqueue(); - fclose(psend); - fclose(precv); - while ((pid = wait(0)) != -1 && pid != childpid) - ; + close_process(PDesc); childpid = -1; } @@ -748,7 +738,7 @@ caldone() /* done with calculation */ rcalfill(x, y) /* fill with ray-calculated pixel */ int x, y; { - if (queuesiz >= PACKSIZ) /* flush queue if needed */ + if (queuesiz >= packsiz) /* flush queue if needed */ clearqueue(); /* add position to queue */ queue[queuesiz][0] = x; @@ -760,43 +750,48 @@ int x, y; clearqueue() /* process queue */ { FVECT orig, dir; - float fbuf[6]; + float fbuf[6*(PACKSIZ+1)]; + register float *fbp; register int i; + if (queuesiz == 0) + return; + fbp = fbuf; for (i = 0; i < queuesiz; i++) { viewray(orig, dir, &ourview, (queue[i][0]+.5)/hresolu, (queue[i][1]+.5)/vresolu); - fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; - fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; - fwrite((char *)fbuf, sizeof(float), 6, psend); + *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; + *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; } - /* flush output and get results */ - fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ - fwrite((char *)fbuf, sizeof(float), 6, psend); - if (fflush(psend) == EOF) - syserror(); + /* mark end and get results */ + bzero((char *)fbp, 6*sizeof(float)); + if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, + 6*sizeof(float)*(queuesiz+1)) != + 4*sizeof(float)*queuesiz) { + fprintf(stderr, "%s: error reading from rtrace process\n", + progname); + exit(1); + } + fbp = fbuf; for (i = 0; i < queuesiz; i++) { - if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { - fprintf(stderr, "%s: read error in clearqueue\n", - progname); - exit(1); - } if (ourexp > 0 && ourexp != 1.0) { - fbuf[0] *= ourexp; - fbuf[1] *= ourexp; - fbuf[2] *= ourexp; + fbp[0] *= ourexp; + fbp[1] *= ourexp; + fbp[2] *= ourexp; } setcolr(pscan(queue[i][1])[queue[i][0]], - fbuf[0], fbuf[1], fbuf[2]); - zscan(queue[i][1])[queue[i][0]] = fbuf[3]; + fbp[0], fbp[1], fbp[2]); + zscan(queue[i][1])[queue[i][0]] = fbp[3]; + fbp += 4; } queuesiz = 0; } -syserror() /* report error and exit */ +syserror(s) /* report error and exit */ +char *s; { - perror(progname); + perror(s); exit(1); }