--- ray/src/px/pinterp.c 1991/04/18 14:35:26 1.33 +++ ray/src/px/pinterp.c 1992/09/24 21:33:52 2.8 @@ -1,3 +1,5 @@ +/* Copyright (c) 1991 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif @@ -10,12 +12,14 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" -#include +#include #include "view.h" #include "color.h" +#include "resolu.h" + #ifndef BSD #define vfork fork #endif @@ -26,9 +30,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 +62,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 +78,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 +96,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,25 +147,27 @@ 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]); @@ -180,6 +188,8 @@ char *argv[]; /* check arguments */ if ((argc-i)%2) goto userr; + if (fillsamp == 1) + fillo &= ~F_BACK; /* set view */ if (err = setview(&ourview)) { fprintf(stderr, "%s: %s\n", progname, err); @@ -187,10 +197,11 @@ char *argv[]; } 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(); + bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); /* get input */ for ( ; i < argc; i += 2) addpicture(argv[i], argv[i+1]); @@ -233,8 +244,6 @@ 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)) { @@ -249,19 +258,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; @@ -279,9 +283,7 @@ char *pfile, *zspec; 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,9 +298,10 @@ 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(); /* get z specification or file */ @@ -309,17 +312,18 @@ char *pfile, *zspec; perror(zspec); exit(1); } - for (x = 0; x < thresolu; x++) + 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); } @@ -388,9 +392,8 @@ struct position *lasty; /* input/output */ 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 */ @@ -503,7 +506,7 @@ FVECT pos; 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); @@ -621,7 +624,7 @@ 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(); @@ -690,57 +693,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)) cp++; + if (!*cp) break; + *wp++ = cp; + while (!isspace(*cp)) + if (!*cp++) goto done; + *cp++ = '\0'; } - if (childpid == -1) +done: + *wp = NULL; + /* start process */ + if ((rval = open_process(PDesc, argv)) < 0) syserror(); - close(p0[0]); - close(p1[1]); - if ((psend = fdopen(p0[1], "w")) == NULL) - syserror(); - if ((precv = fdopen(p1[0], "r")) == NULL) - syserror(); + 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 +746,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,36 +758,38 @@ int x, y; clearqueue() /* process queue */ { FVECT orig, dir; - float fbuf[6]; + float fbuf[6*(PACKSIZ+1)]; + register float *fbp; register int i; + 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; }