--- ray/src/px/pinterp.c 1990/01/04 17:47:37 1.14 +++ ray/src/px/pinterp.c 1992/06/19 12:54:41 2.7 @@ -1,3 +1,5 @@ +/* Copyright (c) 1991 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif @@ -10,24 +12,39 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" +#include + +#include + #include "view.h" #include "color.h" -#define pscan(y) (ourpict+(y)*ourview.hresolu) -#define zscan(y) (ourzbuf+(y)*ourview.hresolu) +#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 -x %d %s" +#define RTCOM "rtrace -h- -ovl -fff " #define ABS(x) ((x)>0?(x):-(x)) -VIEW ourview = STDVIEW(512); /* desired view */ +struct position {int x,y; float z;}; +VIEW ourview = STDVIEW; /* desired view */ +int hresolu = 512; /* horizontal resolution */ +int vresolu = 512; /* vertical resolution */ +double pixaspect = 1.0; /* pixel aspect ratio */ + double zeps = .02; /* allowed z epsilon */ COLR *ourpict; /* output picture */ @@ -35,21 +52,27 @@ float *ourzbuf; /* corresponding z-buffer */ char *progname; -VIEW theirview = STDVIEW(512); /* input view */ -int gotview; /* got input view? */ - -int fill = F_FORE|F_BACK; /* selected fill algorithm */ +int fillo = F_FORE|F_BACK; /* selected fill options */ +int fillsamp = 0; /* sample separation (0 == inf) */ extern int backfill(), rcalfill(); /* fill functions */ -int (*deffill)() = backfill; /* selected fill function */ +int (*fillfunc)() = backfill; /* selected fill function */ COLR backcolr = BLKCOLR; /* background color */ double backz = 0.0; /* background z value */ +int normdist = 1; /* normalized distance? */ +double ourexp = -1; /* output picture exposure */ +VIEW theirview = STDVIEW; /* input view */ +int gotview; /* got input view? */ +int wrongformat = 0; /* input in another format? */ +RESOLU tresolu; /* input resolution */ +double theirexp; /* input picture exposure */ double theirs2ours[4][4]; /* transformation matrix */ -int normdist = 1; /* normalized distance? */ +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 */ @@ -57,58 +80,68 @@ 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; - int i; + int i, rval; progname = argv[0]; - for (i = 1; i < argc && argv[i][0] == '-'; i++) + for (i = 1; i < argc && argv[i][0] == '-'; i++) { + rval = getviewopt(&ourview, argc-i, argv+i); + if (rval >= 0) { + i += rval; + continue; + } 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); - fill = 0; + check(3,NULL); + fillo = 0; break; case 'f': /* foreground */ - check(3,0); - fill = F_FORE; + check(3,NULL); + fillo = F_FORE; break; case 'b': /* background */ - check(3,0); - fill = F_BACK; + check(3,NULL); + fillo = F_BACK; break; case 'a': /* all */ - check(3,0); - fill = F_FORE|F_BACK; + check(3,NULL); + fillo = F_FORE|F_BACK; break; + case 's': /* sample */ + check(3,"i"); + fillsamp = atoi(argv[++i]); + break; case 'c': /* color */ - check(3,3); - deffill = backfill; + 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); - deffill = backfill; + check(3,"f"); + fillfunc = backfill; backz = atof(argv[++i]); break; case 'r': /* rtrace */ - check(3,1); - deffill = rcalfill; + check(3,"s"); + fillfunc = rcalfill; calstart(RTCOM, argv[++i]); break; default: @@ -116,63 +149,35 @@ char *argv[]; } break; case 'z': /* z file */ - check(2,1); + check(2,"s"); zfile = argv[++i]; break; case 'x': /* x resolution */ - check(2,1); - ourview.hresolu = atoi(argv[++i]); + check(2,"i"); + hresolu = atoi(argv[++i]); break; case 'y': /* y resolution */ - check(2,1); - ourview.vresolu = atoi(argv[++i]); + check(2,"i"); + vresolu = atoi(argv[++i]); break; - case 'v': /* view */ - switch (argv[i][2]) { - case 't': /* type */ - check(4,0); - ourview.type = argv[i][3]; - break; - case 'p': /* point */ - check(3,3); - ourview.vp[0] = atof(argv[++i]); - ourview.vp[1] = atof(argv[++i]); - ourview.vp[2] = atof(argv[++i]); - break; - case 'd': /* direction */ - check(3,3); - ourview.vdir[0] = atof(argv[++i]); - ourview.vdir[1] = atof(argv[++i]); - ourview.vdir[2] = atof(argv[++i]); - break; - case 'u': /* up */ - check(3,3); - ourview.vup[0] = atof(argv[++i]); - ourview.vup[1] = atof(argv[++i]); - ourview.vup[2] = atof(argv[++i]); - break; - case 'h': /* horizontal */ - check(3,1); - ourview.horiz = atof(argv[++i]); - break; - case 'v': /* vertical */ - check(3,1); - ourview.vert = atof(argv[++i]); - break; - case 'f': /* file */ - check(3,1); - gotvfile = viewfile(argv[++i], &ourview); - if (gotvfile < 0) { - perror(argv[i]); - exit(1); - } else if (gotvfile == 0) { - fprintf(stderr, "%s: bad view file\n", - argv[i]); - exit(1); - } - break; - default: + case 'p': /* pixel aspect */ + 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,"s"); + gotvfile = viewfile(argv[++i], &ourview, 0, 0); + if (gotvfile < 0) { + perror(argv[i]); + exit(1); + } else if (gotvfile == 0) { + fprintf(stderr, "%s: bad view file\n", + argv[i]); + exit(1); } break; default: @@ -181,39 +186,47 @@ char *argv[]; progname, argv[i]); goto userr; } + } /* 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); exit(1); } + normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); /* allocate frame */ - ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); - ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); - if (ourpict == NULL || ourzbuf == NULL) { - perror(progname); - exit(1); - } + 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]); /* fill in spaces */ - if (fill&F_BACK) - backpicture(); + if (fillo&F_BACK) + backpicture(fillfunc, fillsamp); else - fillpicture(); + fillpicture(fillfunc); /* close calculation */ caldone(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { - printf(VIEWSTR); + fputs(VIEWSTR, stdout); fprintview(&ourview, stdout); - printf("\n"); + putc('\n', stdout); } - printf("\n"); + if (pixaspect < .99 || pixaspect > 1.01) + fputaspect(pixaspect, stdout); + if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) + fputexpos(ourexp, stdout); + fputformat(COLRFMT, stdout); + putc('\n', stdout); /* write picture */ writepicture(); /* write z file */ @@ -223,7 +236,7 @@ char *argv[]; exit(0); userr: fprintf(stderr, - "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", + "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n", progname); exit(1); #undef check @@ -233,92 +246,98 @@ userr: headline(s) /* process header string */ char *s; { - static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; - register char **an; + char fmt[32]; - printf("\t%s", s); + if (isformat(s)) { + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + return; + } + putc('\t', stdout); + fputs(s, stdout); - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&theirview, s+strlen(*an)) == 0) - gotview++; - break; - } + if (isexpos(s)) { + theirexp *= exposval(s); + return; + } + if (isview(s) && sscanview(&theirview, s) > 0) + gotview++; } addpicture(pfile, zspec) /* add picture to output */ char *pfile, *zspec; { - extern double atof(); - FILE *pfp, *zfp; + FILE *pfp; + int zfd; char *err; COLR *scanin; - float *zin, *zlast; - int *plast; + float *zin; + struct position *plast; int y; /* open picture file */ if ((pfp = fopen(pfile, "r")) == NULL) { perror(pfile); exit(1); } - /* get header and view */ - printf("%s:\n", pfile); + /* get header with exposure and view */ + theirexp = 1.0; gotview = 0; - getheader(pfp, headline); - if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) - != (YMAJOR|YDECR)) { - fprintf(stderr, "%s: picture view error\n", pfile); + printf("%s:\n", pfile); + getheader(pfp, headline, NULL); + if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) { + fprintf(stderr, "%s: picture format error\n", pfile); exit(1); } + if (ourexp <= 0) + ourexp = theirexp; + else if (ABS(theirexp-ourexp) > .01*ourexp) + fprintf(stderr, "%s: different exposure (warning)\n", pfile); if (err = setview(&theirview)) { fprintf(stderr, "%s: %s\n", pfile, err); exit(1); } /* compute transformation */ - pixform(theirs2ours, &theirview, &ourview); + hasmatrix = pixform(theirs2ours, &theirview, &ourview); /* allocate scanlines */ - scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); - zin = (float *)malloc(theirview.hresolu*sizeof(float)); - plast = (int *)calloc(theirview.hresolu, sizeof(int)); - zlast = (float *)calloc(theirview.hresolu, sizeof(float)); - if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { - perror(progname); - exit(1); - } + 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 */ - if ((zfp = fopen(zspec, "r")) == NULL) { + 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 < theirview.hresolu; x++) + for (x = scanlen(&tresolu); x-- > 0; ) zin[x] = zvalue; } /* load image */ - for (y = theirview.vresolu-1; y >= 0; y--) { - if (freadcolrs(scanin, theirview.hresolu, 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 (zfp != NULL - && fread(zin,sizeof(float),theirview.hresolu,zfp) - < theirview.hresolu) { + if (zfd != -1 && read(zfd,(char *)zin, + scanlen(&tresolu)*sizeof(float)) + < scanlen(&tresolu)*sizeof(float)) { fprintf(stderr, "%s: read error\n", zspec); exit(1); } - addscanline(y, scanin, zin, plast, zlast); + addscanline(y, scanin, zin, plast); } /* clean up */ free((char *)scanin); free((char *)zin); free((char *)plast); - free((char *)zlast); fclose(pfp); - if (zfp != NULL) - fclose(zfp); + if (zfd != -1) + close(zfd); } @@ -328,13 +347,17 @@ register VIEW *vw1, *vw2; { double m4t[4][4]; + if (vw1->type != VT_PER && vw1->type != VT_PAR) + return(0); + if (vw2->type != VT_PER && vw2->type != VT_PAR) + return(0); setident4(xfmat); - xfmat[0][0] = vw1->vhinc[0]; - xfmat[0][1] = vw1->vhinc[1]; - xfmat[0][2] = vw1->vhinc[2]; - xfmat[1][0] = vw1->vvinc[0]; - xfmat[1][1] = vw1->vvinc[1]; - xfmat[1][2] = vw1->vvinc[2]; + xfmat[0][0] = vw1->hvec[0]; + xfmat[0][1] = vw1->hvec[1]; + xfmat[0][2] = vw1->hvec[2]; + xfmat[1][0] = vw1->vvec[0]; + xfmat[1][1] = vw1->vvec[1]; + xfmat[1][2] = vw1->vvec[2]; xfmat[2][0] = vw1->vdir[0]; xfmat[2][1] = vw1->vdir[1]; xfmat[2][2] = vw1->vdir[2]; @@ -342,118 +365,168 @@ register VIEW *vw1, *vw2; xfmat[3][1] = vw1->vp[1]; xfmat[3][2] = vw1->vp[2]; setident4(m4t); - m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; - m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; - m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; - m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; - m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; - m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; - m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; - m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; + m4t[0][0] = vw2->hvec[0]/vw2->hn2; + m4t[1][0] = vw2->hvec[1]/vw2->hn2; + m4t[2][0] = vw2->hvec[2]/vw2->hn2; + m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2; + m4t[0][1] = vw2->vvec[0]/vw2->vn2; + m4t[1][1] = vw2->vvec[1]/vw2->vn2; + m4t[2][1] = vw2->vvec[2]/vw2->vn2; + m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2; m4t[0][2] = vw2->vdir[0]; m4t[1][2] = vw2->vdir[1]; m4t[2][2] = vw2->vdir[2]; m4t[3][2] = -DOT(vw2->vp,vw2->vdir); multmat4(xfmat, xfmat, m4t); + return(1); } -addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ +addscanline(y, pline, zline, lasty) /* add scanline to output */ int y; COLR *pline; float *zline; -int *lasty; /* input/output */ -float *lastyz; /* input/output */ +struct position *lasty; /* input/output */ { extern double sqrt(); - double pos[3]; - int lastx = 0; - double lastxz = 0; - double zt; - int xpos, ypos; + FVECT pos; + struct position lastx, newpos; register int x; - for (x = theirview.hresolu-1; x >= 0; x--) { - pos[0] = x - .5*(theirview.hresolu-1); - pos[1] = y - .5*(theirview.vresolu-1); + lastx.z = 0; + for (x = scanlen(&tresolu); x-- > 0; ) { + pix2loc(pos, &tresolu, x, y); pos[2] = zline[x]; - if (theirview.type == VT_PER) { - if (normdist) /* adjust for eye-ray distance */ - pos[2] /= sqrt( 1. - + pos[0]*pos[0]*theirview.vhn2 - + pos[1]*pos[1]*theirview.vvn2 ); - pos[0] *= pos[2]; - pos[1] *= pos[2]; - } - multp3(pos, pos, theirs2ours); - if (pos[2] <= 0) + if (movepixel(pos) < 0) { + lasty[x].z = lastx.z = 0; /* mark invalid */ continue; - if (ourview.type == VT_PER) { - pos[0] /= pos[2]; - pos[1] /= pos[2]; } - pos[0] += .5*ourview.hresolu; - pos[1] += .5*ourview.vresolu; - if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu - || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) - continue; + newpos.x = pos[0] * hresolu; + newpos.y = pos[1] * vresolu; + newpos.z = zline[x]; /* add pixel to our image */ - zt = 2.*zeps*zline[x]; - addpixel(xpos, ypos, - (fill&F_FORE && ABS(zline[x]-lastxz) <= zt) - ? lastx - xpos : 1, - (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) - ? lasty[x] - ypos : 1, - pline[x], pos[2]); - lastx = xpos; - lasty[x] = ypos; - lastxz = lastyz[x] = zline[x]; + if (pos[0] >= 0 && newpos.x < hresolu + && pos[1] >= 0 && newpos.y < vresolu) { + addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); + lasty[x].x = lastx.x = newpos.x; + lasty[x].y = lastx.y = newpos.y; + lasty[x].z = lastx.z = newpos.z; + } else + lasty[x].z = lastx.z = 0; /* mark invalid */ } } -addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ -int xstart, ystart; -int width, height; +addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ +struct position *p0, *p1, *p2; COLR pix; double z; { - register int x, y; - /* make width and height positive */ - if (width < 0) { - width = -width; - xstart = xstart-width+1; - } else if (width == 0) - width = 1; - if (height < 0) { - height = -height; - ystart = ystart-height+1; - } else if (height == 0) - height = 1; - /* fill pixel(s) within rectangle */ - for (y = ystart; y < ystart+height; y++) - for (x = xstart; x < xstart+width; x++) - if (zscan(y)[x] <= 0 - || zscan(y)[x]-z > zeps*zscan(y)[x]) { + double zt = 2.*zeps*p0->z; /* threshold */ + int s1x, s1y, s2x, s2y; /* step sizes */ + int l1, l2, c1, c2; /* side lengths and counters */ + int p1isy; /* p0p1 along y? */ + int x1, y1; /* p1 position */ + register int x, y; /* final position */ + + /* compute vector p0p1 */ + if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) { + s1x = p1->x - p0->x; + s1y = p1->y - p0->y; + l1 = ABS(s1x); + if (p1isy = (ABS(s1y) > l1)) + l1 = ABS(s1y); + } else { + l1 = s1x = s1y = 1; + p1isy = -1; + } + /* compute vector p0p2 */ + if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) { + s2x = p2->x - p0->x; + s2y = p2->y - p0->y; + if (p1isy == 1) + l2 = ABS(s2x); + else { + l2 = ABS(s2y); + if (p1isy != 0 && ABS(s2x) > l2) + l2 = ABS(s2x); + } + } else + l2 = s2x = s2y = 1; + /* fill the parallelogram */ + for (c1 = l1; c1-- > 0; ) { + x1 = p0->x + c1*s1x/l1; + y1 = p0->y + c1*s1y/l1; + for (c2 = l2; c2-- > 0; ) { + x = x1 + c2*s2x/l2; + if (x < 0 || x >= hresolu) + continue; + y = y1 + c2*s2y/l2; + if (y < 0 || y >= vresolu) + continue; + if (zscan(y)[x] <= 0 || zscan(y)[x]-z + > zeps*zscan(y)[x]) { zscan(y)[x] = z; copycolr(pscan(y)[x], pix); } + } + } } -backpicture() /* background fill algorithm */ +movepixel(pos) /* reposition image point */ +FVECT pos; { + FVECT pt, direc; + + if (pos[2] <= 0) /* empty pixel */ + return(-1); + 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]; + } + multp3(pos, pos, theirs2ours); + if (pos[2] <= 0) + return(-1); + if (ourview.type == VT_PER) { + pos[0] /= pos[2]; + pos[1] /= pos[2]; + } + pos[0] += .5 - ourview.hoff; + pos[1] += .5 - ourview.voff; + return(0); + } + if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) + return(-1); + pt[0] += direc[0]*pos[2]; + pt[1] += direc[1]*pos[2]; + pt[2] += direc[2]*pos[2]; + viewloc(pos, &ourview, pt); + if (pos[2] <= 0) + return(-1); + return(0); +} + + +backpicture(fill, samp) /* background fill algorithm */ +int (*fill)(); +int samp; +{ int *yback, xback; int y; - COLR pfill; register int x, i; /* get back buffer */ - yback = (int *)malloc(ourview.hresolu*sizeof(int)); - if (yback == NULL) { - perror(progname); - return; - } - for (x = 0; x < ourview.hresolu; x++) + yback = (int *)malloc(hresolu*sizeof(int)); + if (yback == NULL) + syserror(); + for (x = 0; x < hresolu; x++) yback[x] = -2; /* * Xback and yback are the pixel locations of suitable @@ -462,19 +535,19 @@ backpicture() /* background fill algorithm */ * that there is no suitable background in this direction. */ /* fill image */ - for (y = 0; y < ourview.vresolu; y++) { + for (y = 0; y < vresolu; y++) { xback = -2; - for (x = 0; x < ourview.hresolu; x++) + for (x = 0; x < hresolu; x++) if (zscan(y)[x] <= 0) { /* empty pixel */ /* * First, find background from above or below. * (farthest assigned pixel) */ if (yback[x] == -2) { - for (i = y+1; i < ourview.vresolu; i++) + for (i = y+1; i < vresolu; i++) if (zscan(i)[x] > 0) break; - if (i < ourview.vresolu + if (i < vresolu && (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) yback[x] = i; else @@ -484,38 +557,50 @@ backpicture() /* background fill algorithm */ * Next, find background from left or right. */ if (xback == -2) { - for (i = x+1; i < ourview.hresolu; i++) + for (i = x+1; i < hresolu; i++) if (zscan(y)[i] > 0) break; - if (i < ourview.hresolu + if (i < hresolu && (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) xback = i; else xback = x-1; } /* - * Check to see if we have no background for - * this pixel. If not, use background color. + * If we have no background for this pixel, + * use the given fill function. */ - if (xback < 0 && yback[x] < 0) { - (*deffill)(x,y); - continue; - } + if (xback < 0 && yback[x] < 0) + goto fillit; /* * Compare, and use the background that is * farther, unless one of them is next to us. + * If the background is too distant, call + * the fill function. */ if ( yback[x] < 0 || (xback >= 0 && ABS(x-xback) <= 1) || ( ABS(y-yback[x]) > 1 && zscan(yback[x])[x] < zscan(y)[xback] ) ) { + if (samp > 0 && ABS(x-xback) >= samp) + goto fillit; copycolr(pscan(y)[x],pscan(y)[xback]); zscan(y)[x] = zscan(y)[xback]; } else { + if (samp > 0 && ABS(y-yback[x]) > samp) + goto fillit; copycolr(pscan(y)[x],pscan(yback[x])[x]); zscan(y)[x] = zscan(yback[x])[x]; } + continue; + fillit: + (*fill)(x,y); + if (fill == rcalfill) { /* use it */ + clearqueue(); + xback = x; + yback[x] = y; + } } else { /* full pixel */ yback[x] = -2; xback = -2; @@ -525,14 +610,15 @@ backpicture() /* background fill algorithm */ } -fillpicture() /* paint in empty pixels with default */ +fillpicture(fill) /* paint in empty pixels using fill */ +int (*fill)(); { register int x, y; - for (y = 0; y < ourview.vresolu; y++) - for (x = 0; x < ourview.hresolu; x++) + for (y = 0; y < vresolu; y++) + for (x = 0; x < hresolu; x++) if (zscan(y)[x] <= 0) - (*deffill)(x,y); + (*fill)(x,y); } @@ -540,12 +626,10 @@ writepicture() /* write out picture */ { int y; - fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); - for (y = ourview.vresolu-1; y >= 0; y--) - if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { - perror(progname); - exit(1); - } + fprtresolu(hresolu, vresolu, stdout); + for (y = vresolu-1; y >= 0; y--) + if (fwritecolrs(pscan(y), hresolu, stdout) < 0) + syserror(); } @@ -554,41 +638,39 @@ char *fname; { extern double sqrt(); int donorm = normdist && ourview.type == VT_PER; - FILE *fp; + int fd; int y; float *zout; - if ((fp = fopen(fname, "w")) == NULL) { + if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { perror(fname); exit(1); } if (donorm - && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { - perror(progname); - exit(1); - } - for (y = ourview.vresolu-1; y >= 0; y--) { + && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) + syserror(); + for (y = vresolu-1; y >= 0; y--) { if (donorm) { double vx, yzn2; register int x; - yzn2 = y - .5*(ourview.vresolu-1); - yzn2 = 1. + yzn2*yzn2*ourview.vvn2; - for (x = 0; x < ourview.hresolu; x++) { - vx = x - .5*(ourview.hresolu-1); + yzn2 = (y+.5)/vresolu + ourview.voff - .5; + yzn2 = 1. + yzn2*yzn2*ourview.vn2; + for (x = 0; x < hresolu; x++) { + vx = (x+.5)/hresolu + ourview.hoff - .5; zout[x] = zscan(y)[x] - * sqrt(vx*vx*ourview.vhn2 + yzn2); + * sqrt(vx*vx*ourview.hn2 + yzn2); } } else zout = zscan(y); - if (fwrite(zout, sizeof(float), ourview.hresolu, fp) - < ourview.hresolu) { + if (write(fd, (char *)zout, hresolu*sizeof(float)) + < hresolu*sizeof(float)) { perror(fname); exit(1); } } if (donorm) free((char *)zout); - fclose(fp); + close(fd); } @@ -613,58 +695,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, PACKSIZ, 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; - if (fclose(psend) == EOF) - syserror(); clearqueue(); - fclose(precv); - while ((pid = wait(0)) != -1 && pid != childpid) - ; + close_process(PDesc); childpid = -1; } @@ -672,41 +748,50 @@ caldone() /* done with calculation */ rcalfill(x, y) /* fill with ray-calculated pixel */ int x, y; { - FVECT orig, dir; - float outbuf[6]; - - if (queuesiz >= PACKSIZ) { /* flush queue */ - if (fflush(psend) == EOF) - syserror(); + if (queuesiz >= packsiz) /* flush queue if needed */ clearqueue(); - } - /* send new ray */ - rayview(orig, dir, &ourview, x+.5, y+.5); - outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; - outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; - if (fwrite(outbuf, sizeof(float), 6, psend) < 6) - syserror(); - /* remember it */ + /* add position to queue */ queue[queuesiz][0] = x; queue[queuesiz][1] = y; queuesiz++; } -clearqueue() /* get results from queue */ +clearqueue() /* process queue */ { - float inbuf[4]; + FVECT orig, dir; + float fbuf[6*(PACKSIZ+1)]; + register float *fbp; register int i; + fbp = fbuf; for (i = 0; i < queuesiz; i++) { - if (fread(inbuf, sizeof(float), 4, precv) < 4) { - fprintf(stderr, "%s: read error in clearqueue\n", - progname); - exit(1); + viewray(orig, dir, &ourview, + (queue[i][0]+.5)/hresolu, + (queue[i][1]+.5)/vresolu); + *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; + *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; + } + /* 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 (ourexp > 0 && ourexp != 1.0) { + fbp[0] *= ourexp; + fbp[1] *= ourexp; + fbp[2] *= ourexp; } setcolr(pscan(queue[i][1])[queue[i][0]], - inbuf[0], inbuf[1], inbuf[2]); - zscan(queue[i][1])[queue[i][0]] = inbuf[3]; + fbp[0], fbp[1], fbp[2]); + zscan(queue[i][1])[queue[i][0]] = fbp[3]; + fbp += 4; } queuesiz = 0; }