ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glareval.c
(Generate patch)

Comparing ray/src/util/glareval.c (file contents):
Revision 1.10 by greg, Fri Apr 5 14:41:55 1991 UTC vs.
Revision 1.20 by greg, Tue Jul 30 12:56:36 1991 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include "glare.h"
12 < #include <sys/param.h>
13 <                                        /* compute rtrace buffer size */
14 < #ifndef PIPE_BUF
15 < #define PIPE_BUF        512             /* hyperconservative */
16 < #endif
17 < #define MAXPIX          (PIPE_BUF/(6*sizeof(float)) - 1)
12 >                                        /* maximum rtrace buffer size */
13 > #define MAXPIX          (4096/(6*sizeof(float)))
14  
15 < #ifndef BSD
20 < #define vfork           fork
21 < #endif
22 <
23 < #define MAXSBUF         1023980 /* maximum total size of scanline buffer */
15 > #define MAXSBUF         786432  /* maximum total size of scanline buffer */
16   #define HSIZE           317     /* size of scanline hash table */
17   #define NRETIRE         16      /* number of scanlines to retire at once */
18  
19 < int     rt_pid = -1;            /* process id for rtrace */
28 < int     fd_tort, fd_fromrt;     /* pipe descriptors */
19 > int     rt_pd[3] = {-1,-1,-1};  /* process id & descriptors for rtrace */
20  
21   FILE    *pictfp = NULL;         /* picture file pointer */
22   double  exposure;               /* picture exposure */
# Line 44 | Line 35 | typedef struct scan {
35   #define scandata(sl)    ((COLR *)((sl)+1))
36   #define shash(y)        ((y)%HSIZE)
37  
38 + static int      maxpix;         /* maximum number of pixels to buffer */
39 +
40   static SCAN     *freelist;              /* scanline free list */
41   static SCAN     *hashtab[HSIZE];        /* scanline hash table */
42  
43   static long     ncall = 0L;     /* number of calls to getpictscan */
44   static long     nread = 0L;     /* number of scanlines read */
45 + static long     nrecl = 0L;     /* number of scanlines reclaimed */
46  
47 + static int      wrongformat = 0;
48 +
49   SCAN    *scanretire();
50  
51   extern long     ftell();
# Line 75 | Line 71 | int    y;
71                          if (sl->y == y) {               /* reclaim */
72                                  sl->next = hashtab[hi];
73                                  hashtab[hi] = sl;
74 +                                nrecl++;
75                          }
76                          return(sl);
77                  }
# Line 138 | Line 135 | pict_stats()                   /* print out picture read statistics */
135   {
136          static long     lastcall = 0L;  /* ncall at last report */
137          static long     lastread = 0L;  /* nread at last report */
138 +        static long     lastrecl = 0L;  /* nrecl at last report */
139  
140          if (ncall == lastcall)
141                  return;
142 <        fprintf(stderr, "%s: %ld scanlines read in %ld calls\n",
143 <                        progname, nread-lastread, ncall-lastcall);
142 >        fprintf(stderr, "%s: %ld scanlines read (%ld reclaimed) in %ld calls\n",
143 >                progname, nread-lastread, nrecl-lastrecl, ncall-lastcall);
144          lastcall = ncall;
145          lastread = nread;
146 +        lastrecl = nrecl;
147   }
148   #endif
149  
# Line 175 | Line 174 | getviewpix(vh, vv)             /* compute single view pixel */
174   int     vh, vv;
175   {
176          FVECT   dir;
177 <        float   rt_buf[6];
177 >        float   rt_buf[12];
178          double  res;
179  
180          if (compdir(dir, vh, vv) < 0)
# Line 183 | Line 182 | int    vh, vv;
182          npixinvw++;
183          if ((res = pict_val(dir)) >= 0.0)
184                  return(res);
185 <        if (rt_pid == -1) {
185 >        if (rt_pd[0] == -1) {
186                  npixmiss++;
187                  return(-1.0);
188          }
# Line 215 | Line 214 | float  *vb;
214   #endif
215          n = 0;
216          for (vh = -hsize; vh <= hsize; vh++) {
217 <                if (compdir(dir, vh, vv) < 0) { /* off viewable region */
217 >                if (compdir(dir, vh, vv) < 0) {         /* not in view */
218                          vb[vh+hsize] = -1.0;
219                          continue;
220                  }
221                  npixinvw++;
222                  if ((vb[vh+hsize] = pict_val(dir)) >= 0.0)
223                          continue;
224 <                if (rt_pid == -1) {             /* missing information */
224 >                if (rt_pd[0] == -1) {           /* missing information */
225                          npixmiss++;
226                          continue;
227                  }
228                                                  /* send to rtrace */
229 <                if (n >= MAXPIX) {                      /* flush */
229 >                if (n >= maxpix) {                      /* flush */
230                          rt_compute(rt_buf, n);
231                          while (n-- > 0)
232                                  vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
# Line 256 | Line 255 | rt_compute(pb, np)             /* process buffer through rtrace *
255   float   *pb;
256   int     np;
257   {
259        static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
260
258   #ifdef DEBUG
259          if (verbose && np > 1)
260                  fprintf(stderr, "%s: sending %d samples to rtrace...\n",
261                                  progname, np);
262   #endif
263 <        if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np
264 <                || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) {
265 <                fprintf(stderr, "%s: error writing to rtrace process\n",
263 >        bzero(pb+6*np, 6*sizeof(float));
264 >        if (process(rt_pd, pb, pb, 3*sizeof(float)*np,
265 >                        6*sizeof(float)*(np+1)) < 3*sizeof(float)*np) {
266 >                fprintf(stderr, "%s: rtrace communication error\n",
267                                  progname);
268                  exit(1);
269          }
272        if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np)
273                        < 3*sizeof(float)*np) {
274                fprintf(stderr, "%s: error reading from rtrace process\n",
275                                progname);
276                exit(1);
277        }
270   }
271  
272  
273   getexpos(s)                     /* get exposure from header line */
274   char    *s;
275   {
276 +        char    fmt[32];
277 +
278          if (isexpos(s))
279                  exposure *= exposval(s);
280 +        else if (isformat(s)) {
281 +                formatval(fmt, s);
282 +                wrongformat = strcmp(fmt, COLRFMT);
283 +        }
284   }
285  
286  
287   open_pict(fn)                   /* open picture file */
288   char    *fn;
289   {
292        register int    i;
293
290          if ((pictfp = fopen(fn, "r")) == NULL) {
291                  fprintf("%s: cannot open\n", fn);
292                  exit(1);
293          }
294          exposure = 1.0;
295 <        getheader(pictfp, getexpos);
296 <        if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
297 <                fprintf("%s: bad picture resolution\n", fn);
295 >        getheader(pictfp, getexpos, NULL);
296 >        if (wrongformat ||
297 >                        fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
298 >                fprintf("%s: bad picture format\n", fn);
299                  exit(1);
300          }
301          initscans();
# Line 307 | Line 304 | char   *fn;
304  
305   close_pict()                    /* done with picture */
306   {
310        register int    i;
311
307          if (pictfp == NULL)
308                  return;
309          fclose(pictfp);
# Line 320 | Line 315 | close_pict()                   /* done with picture */
315   fork_rtrace(av)                 /* open pipe and start rtrace */
316   char    *av[];
317   {
318 <        int     p0[2], p1[2];
318 >        int     rval;
319  
320 <        if (pipe(p0) < 0 || pipe(p1) < 0) {
320 >        rval = open_process(rt_pd, av);
321 >        if (rval < 0) {
322                  perror(progname);
323                  exit(1);
324          }
325 <        if ((rt_pid = vfork()) == 0) {          /* if child */
326 <                close(p0[1]);
331 <                close(p1[0]);
332 <                if (p0[0] != 0) {       /* connect p0 to stdin */
333 <                        dup2(p0[0], 0);
334 <                        close(p0[0]);
335 <                }
336 <                if (p1[1] != 0) {       /* connect p1 to stdout */
337 <                        dup2(p1[1], 1);
338 <                        close(p1[1]);
339 <                }
340 <                execvp(av[0], av);
341 <                perror(av[0]);
342 <                _exit(127);
343 <        }
344 <        if (rt_pid == -1) {
345 <                perror(progname);
325 >        if (rval == 0) {
326 >                fprintf(stderr, "%s: command not found\n", av[0]);
327                  exit(1);
328          }
329 <        close(p0[0]);
330 <        close(p1[1]);
331 <        fd_tort = p0[1];
332 <        fd_fromrt = p1[0];
329 >        maxpix = rval/(6*sizeof(float));
330 >        if (maxpix > MAXPIX)
331 >                maxpix = MAXPIX;
332 >        maxpix--;
333   }
334  
335  
336   done_rtrace()                   /* wait for rtrace to finish */
337   {
338 <        int     pid, status;
338 >        int     status;
339  
340 <        if (rt_pid == -1)
341 <                return;
361 <        close(fd_tort);
362 <        close(fd_fromrt);
363 <        while ((pid = wait(&status)) != -1 && pid != rt_pid)
364 <                ;
365 <        if (pid == rt_pid && status != 0) {
340 >        status = close_process(rt_pd);
341 >        if (status > 0) {
342                  fprintf(stderr, "%s: bad status (%d) from rtrace\n",
343                                  progname, status);
344                  exit(1);
345          }
346 <        rt_pid = -1;
346 >        rt_pd[0] = -1;
347   }
348  
349  
374 int
375 readbuf(fd, bpos, siz)
376 int     fd;
377 char    *bpos;
378 int     siz;
379 {
380        register int    cc, nrem = siz;
381
382        while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
383                bpos += cc;
384                nrem -= cc;
385        }
386        if (cc < 0)
387                return(cc);
388        return(siz-nrem);
389 }
390
391
392 int
393 writebuf(fd, bpos, siz)
394 char    *bpos;
395 int     siz;
396 {
397        register int    cc, nrem = siz;
398
399        while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
400                bpos += cc;
401                nrem -= cc;
402        }
403        if (cc < 0)
404                return(cc);
405        return(siz-nrem);
406 }
407
408
350   SCAN *
351   scanretire()                    /* retire old scanlines to free list */
352   {
# Line 468 | Line 409 | initscans()                            /* initialize scanline buffers */
409                                          /* allocate scanline buffers */
410          scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
411   #ifdef ALIGN
412 <        scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1);
412 >        scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1);
413   #endif
414          i = MAXSBUF / scansize;         /* compute number to allocate */
415          if (i > HSIZE)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines