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 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         1023980 /* 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 +
54   SCAN    *scanretire();
55  
56   extern long     ftell();
# Line 75 | Line 76 | int    y;
76                          if (sl->y == y) {               /* reclaim */
77                                  sl->next = hashtab[hi];
78                                  hashtab[hi] = sl;
79 +                                nrecl++;
80                          }
81                          return(sl);
82                  }
# Line 138 | 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 154 | 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 162 | 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 175 | 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 183 | 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 215 | Line 220 | float  *vb;
220   #endif
221          n = 0;
222          for (vh = -hsize; vh <= hsize; vh++) {
223 <                if (compdir(dir, vh, vv) < 0) { /* off viewable region */
223 >                if (compdir(dir, vh, vv) < 0) {         /* not in view */
224                          vb[vh+hsize] = -1.0;
225                          continue;
226                  }
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 256 | Line 261 | rt_compute(pb, np)             /* process buffer through rtrace *
261   float   *pb;
262   int     np;
263   {
259        static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
260
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          }
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        }
276   }
277  
278  
279 + int
280   getexpos(s)                     /* get exposure from header line */
281   char    *s;
282   {
283 +        char    fmt[32];
284 +
285          if (isexpos(s))
286                  exposure *= exposval(s);
287 +        else if (isformat(s)) {
288 +                formatval(fmt, s);
289 +                wrongformat = strcmp(fmt, COLRFMT);
290 +        }
291 +        return(0);
292   }
293  
294  
295   open_pict(fn)                   /* open picture file */
296   char    *fn;
297   {
292        register int    i;
293
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);
304 <        if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
305 <                fprintf("%s: bad picture resolution\n", fn);
303 >        getheader(pictfp, getexpos, NULL);
304 >        if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) {
305 >                fprintf(stderr, "%s: incompatible picture format\n", fn);
306                  exit(1);
307          }
308          initscans();
# Line 307 | Line 311 | char   *fn;
311  
312   close_pict()                    /* done with picture */
313   {
310        register int    i;
311
314          if (pictfp == NULL)
315                  return;
316          fclose(pictfp);
# Line 320 | 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]);
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);
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;
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) {
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  
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
357   SCAN *
358   scanretire()                    /* retire old scanlines to free list */
359   {
# Line 456 | 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 467 | 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 490 | 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