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.14 by greg, Fri Apr 19 17:41:06 1991 UTC vs.
Revision 2.9 by schorsch, Mon Jun 30 14:59:13 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Compute pixels for glare calculation
6   */
7  
8 + #include "copyright.h"
9 +
10 + #include <string.h>
11 +
12 + #include "rtprocess.h" /* Windows: must come first because of conflicts */
13   #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)
14  
15 < #ifndef BSD
16 < #define vfork           fork
21 < #endif
15 >                                        /* maximum rtrace buffer size */
16 > #define MAXPIX          (4096/(6*sizeof(float)))
17  
18 < #define MAXSBUF         524268  /* maximum total size of scanline buffer */
18 > #define MAXSBUF         786432  /* maximum total size of scanline buffer */
19   #define HSIZE           317     /* size of scanline hash table */
20   #define NRETIRE         16      /* number of scanlines to retire at once */
21  
22 < int     rt_pid = -1;            /* process id for rtrace */
28 < int     fd_tort, fd_fromrt;     /* pipe descriptors */
22 > static SUBPROC  rt_pd = SP_INACTIVE; /* process id & descriptors for rtrace */
23  
24   FILE    *pictfp = NULL;         /* picture file pointer */
25   double  exposure;               /* picture exposure */
# Line 44 | Line 38 | typedef struct scan {
38   #define scandata(sl)    ((COLR *)((sl)+1))
39   #define shash(y)        ((y)%HSIZE)
40  
41 + static int      maxpix;         /* maximum number of pixels to buffer */
42 +
43   static SCAN     *freelist;              /* scanline free list */
44   static SCAN     *hashtab[HSIZE];        /* scanline hash table */
45  
46 + static long     scanbufsiz;             /* size of allocated scanline buffer */
47 +
48   static long     ncall = 0L;     /* number of calls to getpictscan */
49   static long     nread = 0L;     /* number of scanlines read */
50 + static long     nrecl = 0L;     /* number of scanlines reclaimed */
51  
52   static int      wrongformat = 0;
53  
# Line 77 | Line 76 | int    y;
76                          if (sl->y == y) {               /* reclaim */
77                                  sl->next = hashtab[hi];
78                                  hashtab[hi] = sl;
79 < #ifdef DEBUG
81 <                                if (verbose)
82 <                                        fprintf(stderr,
83 <                                                "%s: scanline %d reclaimed\n",
84 <                                                        progname, y);
85 < #endif
79 >                                nrecl++;
80                          }
81                          return(sl);
82                  }
# Line 146 | Line 140 | pict_stats()                   /* print out picture read statistics */
140   {
141          static long     lastcall = 0L;  /* ncall at last report */
142          static long     lastread = 0L;  /* nread at last report */
143 +        static long     lastrecl = 0L;  /* nrecl at last report */
144  
145          if (ncall == lastcall)
146                  return;
147 <        fprintf(stderr, "%s: %ld scanlines read in %ld calls\n",
148 <                        progname, nread-lastread, ncall-lastcall);
147 >        fprintf(stderr, "%s: %ld scanlines read (%ld reclaimed) in %ld calls\n",
148 >                progname, nread-lastread, nrecl-lastrecl, ncall-lastcall);
149          lastcall = ncall;
150          lastread = nread;
151 +        lastrecl = nrecl;
152   }
153   #endif
154  
# Line 162 | Line 158 | pict_val(vd)                   /* find picture value for view directio
158   FVECT   vd;
159   {
160          FVECT   pp;
161 <        double  vpx, vpy, vpz;
161 >        FVECT   ip;
162          COLOR   res;
163  
164          if (pictfp == NULL)
# Line 170 | Line 166 | FVECT  vd;
166          pp[0] = pictview.vp[0] + vd[0];
167          pp[1] = pictview.vp[1] + vd[1];
168          pp[2] = pictview.vp[2] + vd[2];
169 <        viewpixel(&vpx, &vpy, &vpz, &pictview, pp);
170 <        if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.)
169 >        viewloc(ip, &pictview, pp);
170 >        if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. ||
171 >                        ip[1] < 0. || ip[1] >= 1.)
172                  return(-1.0);
173 <        colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]);
173 >        colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]);
174          return(luminance(res)/exposure);
175   }
176  
# Line 183 | Line 180 | getviewpix(vh, vv)             /* compute single view pixel */
180   int     vh, vv;
181   {
182          FVECT   dir;
183 <        float   rt_buf[6];
183 >        float   rt_buf[12];
184          double  res;
185  
186          if (compdir(dir, vh, vv) < 0)
# Line 191 | Line 188 | int    vh, vv;
188          npixinvw++;
189          if ((res = pict_val(dir)) >= 0.0)
190                  return(res);
191 <        if (rt_pid == -1) {
191 >        if (rt_pd.r == -1) {
192                  npixmiss++;
193                  return(-1.0);
194          }
# Line 230 | Line 227 | float  *vb;
227                  npixinvw++;
228                  if ((vb[vh+hsize] = pict_val(dir)) >= 0.0)
229                          continue;
230 <                if (rt_pid == -1) {             /* missing information */
230 >                if (rt_pd.r == -1) {            /* missing information */
231                          npixmiss++;
232                          continue;
233                  }
234                                                  /* send to rtrace */
235 <                if (n >= MAXPIX) {                      /* flush */
235 >                if (n >= maxpix) {                      /* flush */
236                          rt_compute(rt_buf, n);
237                          while (n-- > 0)
238                                  vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
# Line 264 | Line 261 | rt_compute(pb, np)             /* process buffer through rtrace *
261   float   *pb;
262   int     np;
263   {
267        static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
268
264   #ifdef DEBUG
265          if (verbose && np > 1)
266                  fprintf(stderr, "%s: sending %d samples to rtrace...\n",
267                                  progname, np);
268   #endif
269 <        if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np
270 <                || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) {
271 <                fprintf(stderr, "%s: error writing to rtrace process\n",
269 >        memset(pb+6*np, '\0', 6*sizeof(float));
270 >        if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1),
271 >                        6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) {
272 >                fprintf(stderr, "%s: rtrace communication error\n",
273                                  progname);
274                  exit(1);
275          }
280        if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np)
281                        < 3*sizeof(float)*np) {
282                fprintf(stderr, "%s: error reading from rtrace process\n",
283                                progname);
284                exit(1);
285        }
276   }
277  
278  
279 + int
280   getexpos(s)                     /* get exposure from header line */
281   char    *s;
282   {
# Line 297 | Line 288 | char   *s;
288                  formatval(fmt, s);
289                  wrongformat = strcmp(fmt, COLRFMT);
290          }
291 +        return(0);
292   }
293  
294  
# Line 304 | Line 296 | open_pict(fn)                  /* open picture file */
296   char    *fn;
297   {
298          if ((pictfp = fopen(fn, "r")) == NULL) {
299 <                fprintf("%s: cannot open\n", fn);
299 >                fprintf(stderr, "%s: cannot open\n", fn);
300                  exit(1);
301          }
302          exposure = 1.0;
303          getheader(pictfp, getexpos, NULL);
304 <        if (wrongformat ||
305 <                        fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
314 <                fprintf("%s: bad picture format\n", fn);
304 >        if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) {
305 >                fprintf(stderr, "%s: incompatible picture format\n", fn);
306                  exit(1);
307          }
308          initscans();
# Line 331 | Line 322 | close_pict()                   /* done with picture */
322   fork_rtrace(av)                 /* open pipe and start rtrace */
323   char    *av[];
324   {
325 <        int     p0[2], p1[2];
325 >        int     rval;
326  
327 <        if (pipe(p0) < 0 || pipe(p1) < 0) {
327 >        rval = open_process(&rt_pd, av);
328 >        if (rval < 0) {
329                  perror(progname);
330                  exit(1);
331          }
332 <        if ((rt_pid = vfork()) == 0) {          /* if child */
333 <                close(p0[1]);
342 <                close(p1[0]);
343 <                if (p0[0] != 0) {       /* connect p0 to stdin */
344 <                        dup2(p0[0], 0);
345 <                        close(p0[0]);
346 <                }
347 <                if (p1[1] != 0) {       /* connect p1 to stdout */
348 <                        dup2(p1[1], 1);
349 <                        close(p1[1]);
350 <                }
351 <                execvp(av[0], av);
352 <                perror(av[0]);
353 <                _exit(127);
354 <        }
355 <        if (rt_pid == -1) {
356 <                perror(progname);
332 >        if (rval == 0) {
333 >                fprintf(stderr, "%s: command not found\n", av[0]);
334                  exit(1);
335          }
336 <        close(p0[0]);
337 <        close(p1[1]);
338 <        fd_tort = p0[1];
339 <        fd_fromrt = p1[0];
336 >        maxpix = rval/(6*sizeof(float));
337 >        if (maxpix > MAXPIX)
338 >                maxpix = MAXPIX;
339 >        maxpix--;
340   }
341  
342  
343   done_rtrace()                   /* wait for rtrace to finish */
344   {
345 <        int     pid, status;
345 >        int     status;
346  
347 <        if (rt_pid == -1)
348 <                return;
372 <        close(fd_tort);
373 <        close(fd_fromrt);
374 <        while ((pid = wait(&status)) != -1 && pid != rt_pid)
375 <                ;
376 <        if (pid == rt_pid && status != 0) {
347 >        status = close_process(&rt_pd);
348 >        if (status > 0) {
349                  fprintf(stderr, "%s: bad status (%d) from rtrace\n",
350                                  progname, status);
351                  exit(1);
352          }
353 <        rt_pid = -1;
353 >        rt_pd.r = -1;
354   }
355  
356  
385 int
386 readbuf(fd, bpos, siz)
387 int     fd;
388 char    *bpos;
389 int     siz;
390 {
391        register int    cc, nrem = siz;
392
393        while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
394                bpos += cc;
395                nrem -= cc;
396        }
397        if (cc < 0)
398                return(cc);
399        return(siz-nrem);
400 }
401
402
403 int
404 writebuf(fd, bpos, siz)
405 char    *bpos;
406 int     siz;
407 {
408        register int    cc, nrem = siz;
409
410        while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
411                bpos += cc;
412                nrem -= cc;
413        }
414        if (cc < 0)
415                return(cc);
416        return(siz-nrem);
417 }
418
419
357   SCAN *
358   scanretire()                    /* retire old scanlines to free list */
359   {
# Line 467 | Line 404 | initscans()                            /* initialize scanline buffers */
404          register SCAN   *ptr;
405          register int    i;
406                                          /* initialize positions */
407 <        scanpos = (long *)malloc(pysiz*sizeof(long));
407 >        scanpos = (long *)bmalloc(pysiz*sizeof(long));
408          if (scanpos == NULL)
409                  memerr("scanline positions");
410          for (i = pysiz-1; i >= 0; i--)
# Line 478 | Line 415 | initscans()                            /* initialize scanline buffers */
415                  hashtab[i] = NULL;
416                                          /* allocate scanline buffers */
417          scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
418 < #ifdef ALIGN
419 <        scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1);
418 > #ifdef ALIGNT
419 >        scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1);
420   #endif
421          i = MAXSBUF / scansize;         /* compute number to allocate */
422          if (i > HSIZE)
423                  i = HSIZE;
424 <        scan_buf = malloc(i*scansize);  /* get in one big chunk */
424 >        scanbufsiz = i*scansize;
425 >        scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */
426          if (scan_buf == NULL)
427                  memerr("scanline buffers");
428          ptr = (SCAN *)scan_buf;
# Line 501 | Line 439 | initscans()                            /* initialize scanline buffers */
439  
440   donescans()                             /* free up scanlines */
441   {
442 <        free(scan_buf);
443 <        free((char *)scanpos);
442 >        bfree(scan_buf, scanbufsiz);
443 >        bfree((char *)scanpos, pysiz*sizeof(long));
444   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines