--- ray/src/px/pinterp.c 1991/12/19 14:51:55 2.2 +++ ray/src/px/pinterp.c 1994/12/23 17:23:05 2.17 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1994 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -12,7 +12,7 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" -#include +#include #include "view.h" @@ -20,24 +20,25 @@ static char SCCSid[] = "$SunId$ LBL"; #include "resolu.h" -#ifndef BSD -#define vfork fork -#endif - #define pscan(y) (ourpict+(y)*hresolu) #define zscan(y) (ourzbuf+(y)*hresolu) #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)) struct position {int x,y; float z;}; +#define NSTEPS 64 /* number steps in overlap prescan */ +#define MINSTEP 4 /* minimum worthwhile preview step */ + +struct bound {int min,max;}; + VIEW ourview = STDVIEW; /* desired view */ int hresolu = 512; /* horizontal resolution */ int vresolu = 512; /* vertical resolution */ @@ -67,9 +68,10 @@ 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 */ @@ -77,7 +79,9 @@ main(argc, argv) /* interpolate pictures */ int argc; char *argv[]; { -#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt +#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; @@ -93,49 +97,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; @@ -144,30 +148,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); @@ -183,17 +188,22 @@ char *argv[]; /* check arguments */ if ((argc-i)%2) goto userr; + if (fillsamp == 1) + fillo &= ~F_BACK; /* set view */ - if (err = setview(&ourview)) { + if ((err = setview(&ourview)) != 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]); @@ -204,6 +214,8 @@ char *argv[]; fillpicture(fillfunc); /* close calculation */ caldone(); + /* aft clipping */ + clipaft(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { @@ -236,12 +248,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; } @@ -252,12 +263,8 @@ 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++; } @@ -270,12 +277,11 @@ char *pfile, *zspec; COLR *scanin; float *zin; struct position *plast; + struct bound *xlim, ylim; 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; @@ -295,39 +301,59 @@ char *pfile, *zspec; } /* compute transformation */ hasmatrix = pixform(theirs2ours, &theirview, &ourview); - /* allocate scanlines */ - 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 */ + zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); + if (zin == NULL) + syserror(progname); if ((zfd = open(zspec, O_RDONLY)) == -1) { double zvalue; register int x; - if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { - perror(zspec); - exit(1); - } + if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) + syserror(zspec); for (x = scanlen(&tresolu); x-- > 0; ) zin[x] = zvalue; } - /* load image */ - for (y = 0; y < numscans(&tresolu); y++) { + /* compute transferrable perimeter */ + xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound)); + if (xlim == NULL) + syserror(progname); + if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */ + free((char *)zin); + free((char *)xlim); + if (zfd != -1) + close(zfd); + fclose(pfp); + return; + } + /* allocate scanlines */ + scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); + plast = (struct position *)calloc(scanlen(&tresolu), + sizeof(struct position)); + if (scanin == NULL | plast == NULL) + syserror(progname); + /* skip to starting point */ + for (y = 0; y < ylim.min; y++) if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { fprintf(stderr, "%s: read error\n", pfile); exit(1); } - if (zfd != -1 && read(zfd,(char *)zin, - scanlen(&tresolu)*sizeof(float)) - < scanlen(&tresolu)*sizeof(float)) { - fprintf(stderr, "%s: read error\n", zspec); + if (zfd != -1 && lseek(zfd, + (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0) + syserror(zspec); + /* load image */ + for (y = ylim.min; y <= ylim.max; y++) { + if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { + fprintf(stderr, "%s: read error\n", pfile); exit(1); } - addscanline(y, scanin, zin, plast); + if (zfd != -1 && read(zfd, (char *)zin, + scanlen(&tresolu)*sizeof(float)) + < scanlen(&tresolu)*sizeof(float)) + syserror(zspec); + addscanline(xlim+y, y, scanin, zin, plast); } /* clean up */ + free((char *)xlim); free((char *)scanin); free((char *)zin); free((char *)plast); @@ -378,19 +404,19 @@ register VIEW *vw1, *vw2; } -addscanline(y, pline, zline, lasty) /* add scanline to output */ +addscanline(xl, y, pline, zline, lasty) /* add scanline to output */ +struct bound *xl; int y; 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 = scanlen(&tresolu); x-- > 0; ) { + for (x = xl->max; x >= xl->min; x--) { pix2loc(pos, &tresolu, x, y); pos[2] = zline[x]; if (movepixel(pos) < 0) { @@ -470,21 +496,107 @@ double z; } +getperim(xl, yl, zline, zfd) /* compute overlapping image area */ +register struct bound *xl; +struct bound *yl; +float *zline; +int zfd; +{ + int step; + FVECT pos; + register int x, y; + /* set up step size */ + if (scanlen(&tresolu) < numscans(&tresolu)) + step = scanlen(&tresolu)/NSTEPS; + else + step = numscans(&tresolu)/NSTEPS; + if (step < MINSTEP) { /* not worth cropping? */ + yl->min = 0; + yl->max = numscans(&tresolu) - 1; + x = scanlen(&tresolu) - 1; + for (y = numscans(&tresolu); y--; ) { + xl[y].min = 0; + xl[y].max = x; + } + return(1); + } + yl->min = 32000; yl->max = 0; /* search for points on image */ + for (y = step - 1; y < numscans(&tresolu); y += step) { + if (zfd != -1) { + if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float), + 0) < 0) + syserror("lseek"); + if (read(zfd, (char *)zline, + scanlen(&tresolu)*sizeof(float)) + < scanlen(&tresolu)*sizeof(float)) + syserror("read"); + } + xl[y].min = 32000; xl[y].max = 0; /* x max */ + for (x = scanlen(&tresolu); (x -= step) > 0; ) { + pix2loc(pos, &tresolu, x, y); + pos[2] = zline[x]; + if (movepixel(pos) == 0 && pos[0] >= 0 && + pos[0] < 1 && pos[1] >= 0 && + pos[1] < 1) { + xl[y].max = x + step - 1; + xl[y].min = x - step + 1; /* x min */ + if (xl[y].min < 0) + xl[y].min = 0; + for (x = step - 1; x < xl[y].max; x += step) { + pix2loc(pos, &tresolu, x, y); + pos[2] = zline[x]; + if (movepixel(pos) == 0 && + pos[0] >= 0 && + pos[0] < 1 && + pos[1] >= 0 && + pos[1] < 1) { + xl[y].min = x - step + 1; + break; + } + } + if (y < yl->min) /* y limits */ + yl->min = y - step + 1; + yl->max = y + step - 1; + break; + } + } + /* fill in between */ + if (xl[y].min < xl[y-step].min) + xl[y-1].min = xl[y].min; + else + xl[y-1].min = xl[y-step].min; + if (xl[y].max > xl[y-step].max) + xl[y-1].max = xl[y].max; + else + xl[y-1].max = xl[y-step].max; + for (x = 2; x < step; x++) + copystruct(xl+y-x, xl+y-1); + } + if (yl->max >= numscans(&tresolu)) + yl->max = numscans(&tresolu) - 1; + for (x = numscans(&tresolu) - 1; x > y; x--) /* fill bottom rows */ + copystruct(xl+x, xl+y); + return(yl->max >= yl->min); +} + + movepixel(pos) /* reposition image point */ FVECT pos; { + double d0, d1; FVECT pt, direc; if (pos[2] <= 0) /* empty pixel */ return(-1); + if (normdist && theirview.type == VT_PER) { /* adjust distance */ + d0 = pos[0] + theirview.hoff - .5; + d1 = pos[1] + theirview.voff - .5; + pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2); + } if (hasmatrix) { pos[0] += theirview.hoff - .5; pos[1] += theirview.voff - .5; if (theirview.type == VT_PER) { - if (normdist) /* adjust for eye-ray distance */ - pos[2] /= sqrt( 1. - + pos[0]*pos[0]*theirview.hn2 - + pos[1]*pos[1]*theirview.vn2 ); pos[0] *= pos[2]; pos[1] *= pos[2]; } @@ -499,7 +611,7 @@ 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]; @@ -521,7 +633,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; /* @@ -618,6 +730,37 @@ int (*fill)(); } +clipaft() /* perform aft clipping as indicated */ +{ + register int x, y; + double tstdist; + double yzn2, vx; + + if (ourview.vaft <= FTINY) + return; + tstdist = ourview.vaft - ourview.vfore; + for (y = 0; y < vresolu; y++) { + if (ourview.type == VT_PER) { /* adjust distance */ + yzn2 = (y+.5)/vresolu + ourview.voff - .5; + yzn2 = 1. + yzn2*yzn2*ourview.vn2; + tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2); + } + for (x = 0; x < hresolu; x++) + if (zscan(y)[x] > tstdist) { + if (ourview.type == VT_PER) { + vx = (x+.5)/hresolu + ourview.hoff - .5; + if (zscan(y)[x] <= (ourview.vaft - + ourview.vfore) * + sqrt(vx*vx*ourview.hn2 + yzn2)) + continue; + } + bzero(pscan(y)[x], sizeof(COLR)); + zscan(y)[x] = 0.0; + } + } +} + + writepicture() /* write out picture */ { int y; @@ -625,26 +768,23 @@ writepicture() /* write out picture */ 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; @@ -659,10 +799,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); @@ -691,57 +829,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; } @@ -749,7 +882,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; @@ -761,43 +894,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); }