--- ray/src/util/glareval.c 1991/06/26 13:35:16 1.18 +++ ray/src/util/glareval.c 2018/01/24 04:39:52 2.15 @@ -1,31 +1,27 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: glareval.c,v 2.15 2018/01/24 04:39:52 greg Exp $"; #endif - /* * Compute pixels for glare calculation */ +#include "copyright.h" + +#include +#include + +#include "platform.h" +#include "rtprocess.h" /* Windows: must come first because of conflicts */ #include "glare.h" -#include - /* compute rtrace buffer size */ -#ifndef PIPE_BUF -#define PIPE_BUF 512 /* hyperconservative */ -#endif -#define MAXPIX (PIPE_BUF/(6*sizeof(float)) - 1) -#ifndef BSD -#define vfork fork -#endif + /* maximum rtrace buffer size */ +#define MAXPIX (4096/(6*sizeof(float))) #define MAXSBUF 786432 /* maximum total size of scanline buffer */ #define HSIZE 317 /* size of scanline hash table */ #define NRETIRE 16 /* number of scanlines to retire at once */ -int rt_pid = -1; /* process id for rtrace */ -int fd_tort, fd_fromrt; /* pipe descriptors */ +static SUBPROC rt_pd = SP_INACTIVE; /* process id & descriptors for rtrace */ FILE *pictfp = NULL; /* picture file pointer */ double exposure; /* picture exposure */ @@ -44,23 +40,33 @@ typedef struct scan { #define scandata(sl) ((COLR *)((sl)+1)) #define shash(y) ((y)%HSIZE) +static int maxpix; /* maximum number of pixels to buffer */ + static SCAN *freelist; /* scanline free list */ static SCAN *hashtab[HSIZE]; /* scanline hash table */ +static long scanbufsiz; /* size of allocated scanline buffer */ + static long ncall = 0L; /* number of calls to getpictscan */ static long nread = 0L; /* number of scanlines read */ static long nrecl = 0L; /* number of scanlines reclaimed */ static int wrongformat = 0; -SCAN *scanretire(); +static SCAN * claimscan(int y); +static COLR * getpictscan(int y); +static double pict_val(FVECT vd); +static void rt_compute(float *pb, int np); +static gethfunc getexpos; +static SCAN * scanretire(void); +static void initscans(void); +static void donescans(void); -extern long ftell(); - -SCAN * -claimscan(y) /* claim scanline from buffers */ -int y; +static SCAN * +claimscan( /* claim scanline from buffers */ + int y +) { int hi = shash(y); SCAN *slast; @@ -86,9 +92,10 @@ int y; } -COLR * -getpictscan(y) /* get picture scanline */ -int y; +static COLR * +getpictscan( /* get picture scanline */ + int y +) { register SCAN *sl; register int i; @@ -138,7 +145,8 @@ seekerr: #ifdef DEBUG -pict_stats() /* print out picture read statistics */ +void +pict_stats(void) /* print out picture read statistics */ { static long lastcall = 0L; /* ncall at last report */ static long lastread = 0L; /* nread at last report */ @@ -155,12 +163,13 @@ pict_stats() /* print out picture read statistics */ #endif -double -pict_val(vd) /* find picture value for view direction */ -FVECT vd; +static double +pict_val( /* find picture value for view direction */ + FVECT vd +) { FVECT pp; - double vpx, vpy, vpz; + FVECT ip; COLOR res; if (pictfp == NULL) @@ -168,20 +177,21 @@ FVECT vd; pp[0] = pictview.vp[0] + vd[0]; pp[1] = pictview.vp[1] + vd[1]; pp[2] = pictview.vp[2] + vd[2]; - viewpixel(&vpx, &vpy, &vpz, &pictview, pp); - if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) + if (viewloc(ip, &pictview, pp) != 1) return(-1.0); - colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); + colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); return(luminance(res)/exposure); } -double -getviewpix(vh, vv) /* compute single view pixel */ -int vh, vv; +extern double +getviewpix( /* compute single view pixel */ + int vh, + int vv +) { FVECT dir; - float rt_buf[6]; + float rt_buf[12]; double res; if (compdir(dir, vh, vv) < 0) @@ -189,7 +199,7 @@ int vh, vv; npixinvw++; if ((res = pict_val(dir)) >= 0.0) return(res); - if (rt_pid == -1) { + if (rt_pd.r == -1) { npixmiss++; return(-1.0); } @@ -204,9 +214,11 @@ int vh, vv; } -getviewspan(vv, vb) /* compute a span of view pixels */ -int vv; -float *vb; +extern void +getviewspan( /* compute a span of view pixels */ + int vv, + float *vb +) { float rt_buf[6*MAXPIX]; /* rtrace send/receive buffer */ register int n; /* number of pixels in buffer */ @@ -228,15 +240,17 @@ float *vb; npixinvw++; if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) continue; - if (rt_pid == -1) { /* missing information */ + if (rt_pd.r == -1) { /* missing information */ npixmiss++; continue; } /* send to rtrace */ - if (n >= MAXPIX) { /* flush */ + if (n >= maxpix) { /* flush */ rt_compute(rt_buf, n); - while (n-- > 0) + while (n > 0) { + --n; vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); + } } rt_buf[6*n] = ourview.vp[0]; rt_buf[6*n+1] = ourview.vp[1]; @@ -258,34 +272,32 @@ float *vb; } -rt_compute(pb, np) /* process buffer through rtrace */ -float *pb; -int np; +static void +rt_compute( /* process buffer through rtrace */ + float *pb, + int np +) { - static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; - #ifdef DEBUG if (verbose && np > 1) fprintf(stderr, "%s: sending %d samples to rtrace...\n", progname, np); #endif - if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np - || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { - fprintf(stderr, "%s: error writing to rtrace process\n", + memset(pb+6*np, '\0', 6*sizeof(float)); + if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1), + 6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { + fprintf(stderr, "%s: rtrace communication error\n", progname); exit(1); } - if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np) - < 3*sizeof(float)*np) { - fprintf(stderr, "%s: error reading from rtrace process\n", - progname); - exit(1); - } } -getexpos(s) /* get exposure from header line */ -char *s; +static int +getexpos( /* get exposure from header line */ + char *s, + void *p +) { char fmt[32]; @@ -295,28 +307,32 @@ char *s; formatval(fmt, s); wrongformat = strcmp(fmt, COLRFMT); } + return(0); } -open_pict(fn) /* open picture file */ -char *fn; +extern void +open_pict( /* open picture file */ + char *fn +) { if ((pictfp = fopen(fn, "r")) == NULL) { - fprintf("%s: cannot open\n", fn); + fprintf(stderr, "%s: cannot open\n", fn); exit(1); } + SET_FILE_BINARY(pictfp); exposure = 1.0; getheader(pictfp, getexpos, NULL); - if (wrongformat || - fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { - fprintf("%s: bad picture format\n", fn); + if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { + fprintf(stderr, "%s: incompatible picture format\n", fn); exit(1); } initscans(); } -close_pict() /* done with picture */ +extern void +close_pict(void) /* done with picture */ { if (pictfp == NULL) return; @@ -326,98 +342,47 @@ close_pict() /* done with picture */ } -fork_rtrace(av) /* open pipe and start rtrace */ -char *av[]; +extern void +fork_rtrace( /* open pipe and start rtrace */ + char *av[] +) { - int p0[2], p1[2]; + int rval; - if (pipe(p0) < 0 || pipe(p1) < 0) { + rval = open_process(&rt_pd, av); + if (rval < 0) { perror(progname); exit(1); } - if ((rt_pid = vfork()) == 0) { /* if child */ - close(p0[1]); - close(p1[0]); - if (p0[0] != 0) { /* connect p0 to stdin */ - dup2(p0[0], 0); - close(p0[0]); - } - if (p1[1] != 1) { /* connect p1 to stdout */ - dup2(p1[1], 1); - close(p1[1]); - } - execvp(av[0], av); - perror(av[0]); - _exit(127); - } - if (rt_pid == -1) { - perror(progname); + if (rval == 0) { + fprintf(stderr, "%s: command not found\n", av[0]); exit(1); } - close(p0[0]); - close(p1[1]); - fd_tort = p0[1]; - fd_fromrt = p1[0]; + maxpix = rval/(6*sizeof(float)); + if (maxpix > MAXPIX) + maxpix = MAXPIX; + maxpix--; } -done_rtrace() /* wait for rtrace to finish */ +extern void +done_rtrace(void) /* wait for rtrace to finish */ { - int pid, status; + int status; - if (rt_pid == -1) - return; - close(fd_tort); - close(fd_fromrt); - while ((pid = wait(&status)) != -1 && pid != rt_pid) - ; - if (pid == rt_pid && status != 0) { + status = close_process(&rt_pd); + if (status > 0) { fprintf(stderr, "%s: bad status (%d) from rtrace\n", progname, status); exit(1); } - rt_pid = -1; + rt_pd.r = -1; } -int -readbuf(fd, bpos, siz) -int fd; -char *bpos; -int siz; +static SCAN * +scanretire(void) /* retire old scanlines to free list */ { - register int cc, nrem = siz; - - while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { - bpos += cc; - nrem -= cc; - } - if (cc < 0) - return(cc); - return(siz-nrem); -} - - -int -writebuf(fd, bpos, siz) -char *bpos; -int siz; -{ - register int cc, nrem = siz; - - while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { - bpos += cc; - nrem -= cc; - } - if (cc < 0) - return(cc); - return(siz-nrem); -} - - -SCAN * -scanretire() /* retire old scanlines to free list */ -{ SCAN *sold[NRETIRE]; int n; int h; @@ -459,13 +424,14 @@ scanretire() /* retire old scanlines to free list */ static char *scan_buf; -initscans() /* initialize scanline buffers */ +static void +initscans(void) /* initialize scanline buffers */ { int scansize; register SCAN *ptr; register int i; /* initialize positions */ - scanpos = (long *)malloc(pysiz*sizeof(long)); + scanpos = (long *)bmalloc(pysiz*sizeof(long)); if (scanpos == NULL) memerr("scanline positions"); for (i = pysiz-1; i >= 0; i--) @@ -476,13 +442,14 @@ initscans() /* initialize scanline buffers */ hashtab[i] = NULL; /* allocate scanline buffers */ scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); -#ifdef ALIGN - scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1); +#ifdef ALIGNT + scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1); #endif i = MAXSBUF / scansize; /* compute number to allocate */ if (i > HSIZE) i = HSIZE; - scan_buf = malloc(i*scansize); /* get in one big chunk */ + scanbufsiz = i*scansize; + scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ if (scan_buf == NULL) memerr("scanline buffers"); ptr = (SCAN *)scan_buf; @@ -497,8 +464,9 @@ initscans() /* initialize scanline buffers */ } -donescans() /* free up scanlines */ +static void +donescans(void) /* free up scanlines */ { - free(scan_buf); - free((char *)scanpos); + bfree(scan_buf, scanbufsiz); + bfree((char *)scanpos, pysiz*sizeof(long)); }