--- ray/src/px/pinterp.c 1990/01/09 11:39:17 1.18 +++ ray/src/px/pinterp.c 1990/01/18 23:58:29 1.23 @@ -10,10 +10,16 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" +#include + #include "view.h" #include "color.h" +#ifndef BSD +#define vfork fork +#endif + #define pscan(y) (ourpict+(y)*hresolu) #define zscan(y) (ourzbuf+(y)*hresolu) @@ -147,7 +153,7 @@ char *argv[]; if (argv[i][2] != 'f') goto badopt; check(3,1); - gotvfile = viewfile(argv[++i], &ourview); + gotvfile = viewfile(argv[++i], &ourview, 0, 0); if (gotvfile < 0) { perror(argv[i]); exit(1); @@ -241,7 +247,8 @@ 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; @@ -279,7 +286,7 @@ char *pfile, *zspec; 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) { @@ -295,8 +302,8 @@ char *pfile, *zspec; fprintf(stderr, "%s: read error\n", pfile); exit(1); } - if (zfp != NULL - && fread(zin,sizeof(float),thresolu,zfp) < thresolu) { + if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float)) + < thresolu*sizeof(float)) { fprintf(stderr, "%s: read error\n", zspec); exit(1); } @@ -307,8 +314,8 @@ char *pfile, *zspec; free((char *)zin); free((char *)plast); fclose(pfp); - if (zfp != NULL) - fclose(zfp); + if (zfd != -1) + close(zfd); } @@ -359,6 +366,7 @@ struct position *lasty; /* input/output */ struct position lastx, newpos; register int x; + lastx.z = 0; for (x = thresolu-1; x >= 0; x--) { pos[0] = (x+.5)/thresolu + theirview.hoff - .5; pos[1] = (y+.5)/tvresolu + theirview.voff - .5; @@ -560,11 +568,11 @@ 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); } @@ -584,14 +592,15 @@ char *fname; } } else zout = zscan(y); - if (fwrite(zout, sizeof(float), hresolu, fp) < 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); } @@ -662,10 +671,7 @@ caldone() /* done with calculation */ if (childpid == -1) return; - if (fclose(psend) == EOF) - syserror(); - clearqueue(); - fclose(precv); + clearqueue(1); while ((pid = wait(0)) != -1 && pid != childpid) ; childpid = -1; @@ -675,47 +681,49 @@ 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(); - clearqueue(); - } - /* send new ray */ - viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu); - 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 */ + if (queuesiz >= PACKSIZ) /* flush queue if needed */ + clearqueue(0); + /* add position to queue */ queue[queuesiz][0] = x; queue[queuesiz][1] = y; queuesiz++; } -clearqueue() /* get results from queue */ +clearqueue(done) /* process queue */ +int done; { - float inbuf[4]; + FVECT orig, dir; + float fbuf[6]; register int i; for (i = 0; i < queuesiz; i++) { - if (fread(inbuf, sizeof(float), 4, precv) < 4) { + 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); + } + if ((done ? fclose(psend) : fflush(psend)) == EOF) + syserror(); + 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) { - inbuf[0] *= ourexp; - inbuf[1] *= ourexp; - inbuf[2] *= ourexp; + fbuf[0] *= ourexp; + fbuf[1] *= ourexp; + fbuf[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]; + fbuf[0], fbuf[1], fbuf[2]); + zscan(queue[i][1])[queue[i][0]] = fbuf[3]; } + if (done) + fclose(precv); queuesiz = 0; }