--- ray/src/px/pinterp.c 1993/04/01 11:21:18 2.10 +++ ray/src/px/pinterp.c 1994/12/23 17:23:05 2.17 @@ -1,4 +1,4 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1994 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -34,6 +34,11 @@ static char SCCSid[] = "$SunId$ LBL"; 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 */ @@ -186,7 +191,7 @@ char *argv[]; 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); } @@ -197,6 +202,8 @@ char *argv[]; if (ourpict == NULL || ourzbuf == NULL) 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]); @@ -207,6 +214,8 @@ char *argv[]; fillpicture(fillfunc); /* close calculation */ caldone(); + /* aft clipping */ + clipaft(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { @@ -241,8 +250,9 @@ char *s; { char fmt[32]; - if (isformat(s)) { - formatval(fmt, s); + if (isheadid(s)) + return; + if (formatval(fmt, s)) { wrongformat = strcmp(fmt, COLRFMT); return; } @@ -267,6 +277,7 @@ 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) @@ -290,38 +301,59 @@ char *pfile, *zspec; } /* compute transformation */ hasmatrix = pixform(theirs2ours, &theirview, &ourview); - /* allocate scanlines */ - scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); + /* get z specification or file */ zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); - plast = (struct position *)calloc(scanlen(&tresolu), - sizeof(struct position)); - if (scanin == NULL || zin == NULL || plast == NULL) + if (zin == NULL) 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) syserror(zspec); - exit(1); 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); @@ -372,7 +404,8 @@ 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; @@ -383,7 +416,7 @@ struct position *lasty; /* input/output */ 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) { @@ -463,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]; } @@ -492,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]; @@ -611,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; @@ -696,14 +846,14 @@ char *prog, *args; cp = combuf; wp = argv; for ( ; ; ) { - while (isspace(*cp)) cp++; - if (!*cp) break; - *wp++ = cp; - while (!isspace(*cp)) - if (!*cp++) goto done; - *cp++ = '\0'; + while (isspace(*cp)) /* nullify spaces */ + *cp++ = '\0'; + if (!*cp) /* all done? */ + break; + *wp++ = cp; /* add argument to list */ + while (*++cp && !isspace(*cp)) + ; } -done: *wp = NULL; /* start process */ if ((rval = open_process(PDesc, argv)) < 0)