--- ray/src/px/pinterp.c 1990/01/04 10:12:49 1.11 +++ ray/src/px/pinterp.c 1990/01/04 16:52:05 1.13 @@ -17,9 +17,13 @@ static char SCCSid[] = "$SunId$ LBL"; #define pscan(y) (ourpict+(y)*ourview.hresolu) #define zscan(y) (ourzbuf+(y)*ourview.hresolu) -#define F_FORE 1 /* fill foreground */ -#define F_BACK 2 /* fill background */ +#define F_FORE 1 /* fill foreground */ +#define F_BACK 2 /* fill background */ +#define PACKSIZ 42 /* calculation packet size */ + +#define RTCOM "rtrace -h -ovl -fff -x %d %s" + #define ABS(x) ((x)>0?(x):-(x)) VIEW ourview = STDVIEW(512); /* desired view */ @@ -34,13 +38,21 @@ char *progname; VIEW theirview = STDVIEW(512); /* input view */ int gotview; /* got input view? */ -int fill = F_FORE|F_BACK; /* fill level */ +int fill = F_FORE|F_BACK; /* selected fill algorithm */ +extern int backfill(), calfill(); /* fill functions */ +int (*deffill)() = backfill; /* selected fill function */ COLR backcolr = BLKCOLR; /* background color */ +double backz = 0.0; /* background z value */ double theirs2ours[4][4]; /* transformation matrix */ int normdist = 1; /* normalized distance? */ +FILE *psend, *precv; /* pipes to/from fill calculation */ +int childpid; /* child's process id */ +int queue[PACKSIZ][2]; /* pending pixels */ +int queuesiz; /* number of pixels pending */ + main(argc, argv) /* interpolate pictures */ int argc; char *argv[]; @@ -84,10 +96,21 @@ char *argv[]; break; case 'c': /* color */ check(3,3); + deffill = 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; + backz = atof(argv[++i]); + break; + case 'r': /* rtrace */ + check(3,1); + deffill = calfill; + calstart(RTCOM, argv[++i]); + break; default: goto badopt; } @@ -159,7 +182,7 @@ char *argv[]; goto userr; } /* check arguments */ - if (argc-i < 2 || (argc-i)%2) + if ((argc-i)%2) goto userr; /* set view */ if (err = setview(&ourview)) { @@ -167,7 +190,7 @@ char *argv[]; exit(1); } /* allocate frame */ - ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR)); + 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); @@ -178,9 +201,12 @@ char *argv[]; addpicture(argv[i], argv[i+1]); /* fill in spaces */ if (fill&F_BACK) - fillpicture(); - else backpicture(); + else + fillpicture(); + /* close calculation */ + if (deffill == calfill) + caldone(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { @@ -416,7 +442,7 @@ double z; } -fillpicture() /* fill in empty spaces */ +backpicture() /* background fill algorithm */ { int *yback, xback; int y; @@ -473,7 +499,7 @@ fillpicture() /* fill in empty spaces */ * this pixel. If not, use background color. */ if (xback < 0 && yback[x] < 0) { - copycolr(pscan(y)[x], backcolr); + (*deffill)(x,y); continue; } /* @@ -484,10 +510,13 @@ fillpicture() /* fill in empty spaces */ || (xback >= 0 && ABS(x-xback) <= 1) || ( ABS(y-yback[x]) > 1 && zscan(yback[x])[x] - < zscan(y)[xback] ) ) + < zscan(y)[xback] ) ) { copycolr(pscan(y)[x],pscan(y)[xback]); - else + zscan(y)[x] = zscan(y)[xback]; + } else { copycolr(pscan(y)[x],pscan(yback[x])[x]); + zscan(y)[x] = zscan(yback[x])[x]; + } } else { /* full pixel */ yback[x] = -2; xback = -2; @@ -497,14 +526,14 @@ fillpicture() /* fill in empty spaces */ } -backpicture() /* paint in empty pixels */ +fillpicture() /* paint in empty pixels with default */ { register int x, y; for (y = 0; y < ourview.vresolu; y++) for (x = 0; x < ourview.hresolu; x++) if (zscan(y)[x] <= 0) - copycolr(pscan(y)[x], backcolr); + (*deffill)(x,y); } @@ -572,4 +601,103 @@ register char *s; && *s != 'e' && *s != 'E' && *s != '+') return(0); return(1); +} + + +backfill(x, y) /* fill pixel with background */ +int x, y; +{ + register BYTE *dest = pscan(y)[x]; + + copycolr(dest, backcolr); + zscan(y)[x] = backz; +} + + +calstart(prog, args) /* start fill calculation */ +char *prog, *args; +{ + char combuf[512]; + int p0[2], p1[2]; + + sprintf(combuf, prog, PACKSIZ, args); + if (pipe(p0) < 0 || pipe(p1) < 0) + goto syserr; + 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); + } + if (childpid < 0) + goto syserr; + close(p0[0]); + close(p1[1]); + if ((psend = fdopen(p0[1], "w")) == NULL) + goto syserr; + if ((precv = fdopen(p1[0], "r")) == NULL) + goto syserr; + queuesiz = 0; + return; +syserr: + perror(progname); + exit(1); +} + + +caldone() /* done with calculation */ +{ + int pid; + + fclose(psend); + clearqueue(); + fclose(precv); + while ((pid = wait(0)) != -1 && pid != childpid) + ; +} + + +calfill(x, y) /* fill with calculated pixel */ +int x, y; +{ + FVECT orig, dir; + float outbuf[6]; + + if (queuesiz >= PACKSIZ) { /* flush queue */ + fflush(psend); + 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]; + fwrite(outbuf, sizeof(float), 6, psend); + /* remember it */ + queue[queuesiz][0] = x; + queue[queuesiz][1] = y; + queuesiz++; +} + + +clearqueue() /* get results from queue */ +{ + float inbuf[4]; + register int i; + + for (i = 0; i < queuesiz; i++) { + fread(inbuf, sizeof(float), 4, precv); + setcolr(pscan(queue[i][1])[queue[i][0]], + inbuf[0], inbuf[1], inbuf[2]); + zscan(queue[i][1])[queue[i][0]] = inbuf[3]; + } + queuesiz = 0; }