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.1 by greg, Mon Mar 18 12:15:41 1991 UTC vs.
Revision 2.6 by greg, Sat Feb 22 02:07:30 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 "glare.h"
9 < #include <sys/param.h>
10 <                                        /* 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)
9 >                                        /* maximum rtrace buffer size */
10 > #define MAXPIX          (4096/(6*sizeof(float)))
11  
12 < #ifndef BSD
13 < #define vfork           fork
14 < #endif
12 > #define MAXSBUF         786432  /* maximum total size of scanline buffer */
13 > #define HSIZE           317     /* size of scanline hash table */
14 > #define NRETIRE         16      /* number of scanlines to retire at once */
15  
16 < #define NSCANS          16              /* number of scanlines to buffer */
16 > int     rt_pd[3] = {-1,-1,-1};  /* process id & descriptors for rtrace */
17  
25 int     rt_pid = -1;            /* process id for rtrace */
26 int     fd_tort, fd_fromrt;     /* pipe descriptors */
27
18   FILE    *pictfp = NULL;         /* picture file pointer */
19   double  exposure;               /* picture exposure */
20   int     pxsiz, pysiz;           /* picture dimensions */
21 < int     curpos;                 /* current scanline */
22 < long    *scanpos;               /* scanline positions */
23 < struct {
24 <        long    lused;          /* for LRU replacement */
21 >
22 > static int      curpos;         /* current scanline */
23 > static long     *scanpos;       /* scanline positions */
24 >
25 > typedef struct scan {
26          int     y;              /* scanline position */
27 <        COLR    *sl;            /* scanline contents */
28 < } scan[NSCANS];         /* buffered scanlines */
27 >        long    lused;          /* for LRU replacement */
28 >        struct scan     *next;  /* next in this hash or free list */
29 >        /* followed by the scanline data */
30 > } SCAN;                 /* buffered scanline */
31  
32 + #define scandata(sl)    ((COLR *)((sl)+1))
33 + #define shash(y)        ((y)%HSIZE)
34  
35 + static int      maxpix;         /* maximum number of pixels to buffer */
36 +
37 + static SCAN     *freelist;              /* scanline free list */
38 + static SCAN     *hashtab[HSIZE];        /* scanline hash table */
39 +
40 + static long     scanbufsiz;             /* size of allocated scanline buffer */
41 +
42 + static long     ncall = 0L;     /* number of calls to getpictscan */
43 + static long     nread = 0L;     /* number of scanlines read */
44 + static long     nrecl = 0L;     /* number of scanlines reclaimed */
45 +
46 + static int      wrongformat = 0;
47 +
48 + SCAN    *scanretire();
49 +
50 + extern long     ftell();
51 +
52 +
53 + SCAN *
54 + claimscan(y)                    /* claim scanline from buffers */
55 + int     y;
56 + {
57 +        int     hi = shash(y);
58 +        SCAN    *slast;
59 +        register SCAN   *sl;
60 +
61 +        for (sl = hashtab[hi]; sl != NULL; sl = sl->next)
62 +                if (sl->y == y)                         /* active scanline */
63 +                        return(sl);
64 +        for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next)
65 +                if (sl->y == -1 || sl->y == y || sl->next == NULL) {
66 +                        if (slast == NULL)              /* remove from free */
67 +                                freelist = sl->next;
68 +                        else
69 +                                slast->next = sl->next;
70 +                        if (sl->y == y) {               /* reclaim */
71 +                                sl->next = hashtab[hi];
72 +                                hashtab[hi] = sl;
73 +                                nrecl++;
74 +                        }
75 +                        return(sl);
76 +                }
77 +        return(scanretire());           /* need more free scanlines */
78 + }
79 +
80 +
81   COLR *
82   getpictscan(y)                  /* get picture scanline */
83   int     y;
84   {
85 <        extern long     ftell();
45 <        static long     ncall = 0;
46 <        int     minused;
85 >        register SCAN   *sl;
86          register int    i;
87                                          /* first check our buffers */
88 <        ncall++;
89 <        minused = 0;
90 <        for (i = 0; i < NSCANS; i++) {
91 <                if (scan[i].y == y) {
92 <                        scan[i].lused = ncall;
93 <                        return(scan[i].sl);
94 <                }
95 <                if (scan[i].lused < scan[minused].lused)
96 <                        minused = i;
97 <        }
98 <                                        /* not there, read it in */
99 <        if (scanpos[y] == -1) {                 /* need to search */
100 <                if (verbose)
101 <                        fprintf(stderr, "%s: reading picture...\n",
102 <                                        progname);
103 <                while (curpos > y) {
88 >        sl = claimscan(y);
89 >        if (sl == NULL)
90 >                memerr("claimscan()");
91 >        sl->lused = ncall++;
92 >        if (sl->y == y)                 /* scan hit */
93 >                return(scandata(sl));
94 >                                        /* else read in replacement */
95 >        if (scanpos[y] < 0) {                   /* need to search */
96 >                for (i = y+1; i < curpos; i++)
97 >                        if (scanpos[i] >= 0) {
98 >                                if (fseek(pictfp, scanpos[i], 0) < 0)
99 >                                        goto seekerr;
100 >                                curpos = i;
101 >                                break;
102 >                        }
103 >                while (curpos >= y) {
104                          scanpos[curpos] = ftell(pictfp);
105 <                        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
105 >                        if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
106                                  goto readerr;
107 +                        nread++;
108                          curpos--;
109                  }
110 <        } else if (fseek(pictfp, scanpos[y], 0) < 0) {
111 <                fprintf(stderr, "%s: picture seek error\n", progname);
112 <                exit(1);
110 >        } else {
111 >                if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
112 >                        goto seekerr;
113 >                if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
114 >                        goto readerr;
115 >                nread++;
116 >                curpos = y-1;
117          }
118 <        if (verbose)
119 <                fprintf(stderr, "%s: reading scanline %d...\n", progname, y);
120 <        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
121 <                goto readerr;
122 <        curpos = y-1;
79 <        scan[minused].lused = ncall;
80 <        scan[minused].y = y;
81 <        return(scan[minused].sl);
118 >        sl->y = y;
119 >        i = shash(y);                   /* add to hash list */
120 >        sl->next = hashtab[i];
121 >        hashtab[i] = sl;
122 >        return(scandata(sl));
123   readerr:
124          fprintf(stderr, "%s: picture read error\n", progname);
125          exit(1);
126 + seekerr:
127 +        fprintf(stderr, "%s: picture seek error\n", progname);
128 +        exit(1);
129   }
130  
131  
132 + #ifdef DEBUG
133 + pict_stats()                    /* print out picture read statistics */
134 + {
135 +        static long     lastcall = 0L;  /* ncall at last report */
136 +        static long     lastread = 0L;  /* nread at last report */
137 +        static long     lastrecl = 0L;  /* nrecl at last report */
138 +
139 +        if (ncall == lastcall)
140 +                return;
141 +        fprintf(stderr, "%s: %ld scanlines read (%ld reclaimed) in %ld calls\n",
142 +                progname, nread-lastread, nrecl-lastrecl, ncall-lastcall);
143 +        lastcall = ncall;
144 +        lastread = nread;
145 +        lastrecl = nrecl;
146 + }
147 + #endif
148 +
149 +
150   double
151   pict_val(vd)                    /* find picture value for view direction */
152   FVECT   vd;
153   {
154          FVECT   pp;
155 <        double  vpx, vpy, vpz;
155 >        FVECT   ip;
156          COLOR   res;
157  
158          if (pictfp == NULL)
# Line 98 | Line 160 | FVECT  vd;
160          pp[0] = pictview.vp[0] + vd[0];
161          pp[1] = pictview.vp[1] + vd[1];
162          pp[2] = pictview.vp[2] + vd[2];
163 <        viewpixel(&vpx, &vpy, &vpz, &pictview, pp);
164 <        if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.)
163 >        viewloc(ip, &pictview, pp);
164 >        if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. ||
165 >                        ip[1] < 0. || ip[1] >= 1.)
166                  return(-1.0);
167 <        colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]);
167 >        colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]);
168          return(luminance(res)/exposure);
169   }
170  
# Line 111 | 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)
181                  return(-1.0);
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 +        }
189          rt_buf[0] = ourview.vp[0];
190          rt_buf[1] = ourview.vp[1];
191          rt_buf[2] = ourview.vp[2];
# Line 136 | Line 202 | int    vv;
202   float   *vb;
203   {
204          float   rt_buf[6*MAXPIX];       /* rtrace send/receive buffer */
205 <        int     npix_tort;              /* number of pixels in buffer */
205 >        register int    n;              /* number of pixels in buffer */
206          short   buf_vh[MAXPIX];         /* pixel positions */
207          FVECT   dir;
208          register int    vh;
143        register int    i;
209  
210 + #ifdef DEBUG
211          if (verbose)
212                  fprintf(stderr, "%s: computing view span at %d...\n",
213                                  progname, vv);
214 <        npix_tort = 0;
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 (npix_tort >= MAXPIX) {              /* flush */
230 <                        rt_compute(rt_buf, npix_tort);
231 <                        for (i = 0; i < npix_tort; i++)
232 <                                vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
163 <                        npix_tort = 0;
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);
233                  }
234 <                rt_buf[npix_tort] = ourview.vp[0];
235 <                rt_buf[npix_tort+1] = ourview.vp[1];
236 <                rt_buf[npix_tort+2] = ourview.vp[2];
237 <                rt_buf[npix_tort+3] = dir[0];
238 <                rt_buf[npix_tort+4] = dir[1];
239 <                rt_buf[npix_tort+5] = dir[2];
240 <                buf_vh[npix_tort++] = vh;
234 >                rt_buf[6*n] = ourview.vp[0];
235 >                rt_buf[6*n+1] = ourview.vp[1];
236 >                rt_buf[6*n+2] = ourview.vp[2];
237 >                rt_buf[6*n+3] = dir[0];
238 >                rt_buf[6*n+4] = dir[1];
239 >                rt_buf[6*n+5] = dir[2];
240 >                buf_vh[n++] = vh;
241          }
242 <        if (npix_tort > 0) {                    /* process pending buffer */
243 <                rt_compute(rt_buf, npix_tort);
244 <                for (i = 0; i < npix_tort; i++)
245 <                        vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
242 > #ifdef DEBUG
243 >        if (verbose)
244 >                pict_stats();
245 > #endif
246 >        if (n > 0) {                            /* process pending buffer */
247 >                rt_compute(rt_buf, n);
248 >                while (n-- > 0)
249 >                        vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
250          }
251   }
252  
# Line 182 | Line 255 | rt_compute(pb, np)             /* process buffer through rtrace *
255   float   *pb;
256   int     np;
257   {
258 <        static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
186 <
258 > #ifdef DEBUG
259          if (verbose && np > 1)
260                  fprintf(stderr, "%s: sending %d samples to rtrace...\n",
261                                  progname, np);
262 <        if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np
263 <                || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) {
264 <                fprintf(stderr, "%s: error writing to rtrace process\n",
262 > #endif
263 >        bzero(pb+6*np, 6*sizeof(float));
264 >        if (process(rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1),
265 >                        6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) {
266 >                fprintf(stderr, "%s: rtrace communication error\n",
267                                  progname);
268                  exit(1);
269          }
196        if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np)
197                        < 3*sizeof(float)*np) {
198                fprintf(stderr, "%s: error reading from rtrace process\n",
199                                progname);
200                exit(1);
201        }
270   }
271  
272  
273 + int
274   getexpos(s)                     /* get exposure from header line */
275   char    *s;
276   {
277 +        char    fmt[32];
278 +
279          if (isexpos(s))
280                  exposure *= exposval(s);
281 +        else if (isformat(s)) {
282 +                formatval(fmt, s);
283 +                wrongformat = strcmp(fmt, COLRFMT);
284 +        }
285 +        return(0);
286   }
287  
288  
289   open_pict(fn)                   /* open picture file */
290   char    *fn;
291   {
216        register int    i;
217
292          if ((pictfp = fopen(fn, "r")) == NULL) {
293 <                fprintf("%s: cannot open\n", fn);
293 >                fprintf(stderr, "%s: cannot open\n", fn);
294                  exit(1);
295          }
296          exposure = 1.0;
297 <        getheader(pictfp, getexpos);
298 <        if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
299 <                fprintf("%s: bad picture resolution\n", fn);
297 >        getheader(pictfp, getexpos, NULL);
298 >        if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) {
299 >                fprintf(stderr, "%s: incompatible picture format\n", fn);
300                  exit(1);
301          }
302 <        scanpos = (long *)malloc(pysiz*sizeof(long));
229 <        if (scanpos == NULL)
230 <                memerr("scanline positions");
231 <        for (i = 0; i < pysiz; i++)
232 <                scanpos[i] = -1L;
233 <        for (i = 0; i < NSCANS; i++) {
234 <                scan[i].lused = -1;
235 <                scan[i].y = -1;
236 <                scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR));
237 <                if (scan[i].sl == NULL)
238 <                        memerr("scanline buffers");
239 <        }
240 <        curpos = pysiz-1;
302 >        initscans();
303   }
304  
305  
306   close_pict()                    /* done with picture */
307   {
246        register int    i;
247
308          if (pictfp == NULL)
309                  return;
310          fclose(pictfp);
311 <        free((char *)scanpos);
252 <        for (i = 0; i < NSCANS; i++)
253 <                free((char *)scan[i].sl);
311 >        donescans();
312          pictfp = NULL;
313   }
314  
# Line 258 | Line 316 | close_pict()                   /* done with picture */
316   fork_rtrace(av)                 /* open pipe and start rtrace */
317   char    *av[];
318   {
319 <        int     p0[2], p1[2];
319 >        int     rval;
320  
321 <        if (pipe(p0) < 0 || pipe(p1) < 0) {
321 >        rval = open_process(rt_pd, av);
322 >        if (rval < 0) {
323                  perror(progname);
324                  exit(1);
325          }
326 <        if ((rt_pid = vfork()) == 0) {          /* if child */
327 <                close(p0[1]);
269 <                close(p1[0]);
270 <                if (p0[0] != 0) {       /* connect p0 to stdin */
271 <                        dup2(p0[0], 0);
272 <                        close(p0[0]);
273 <                }
274 <                if (p1[1] != 0) {       /* connect p1 to stdout */
275 <                        dup2(p1[1], 1);
276 <                        close(p1[1]);
277 <                }
278 <                execvp(av[0], av);
279 <                perror(av[0]);
280 <                _exit(127);
281 <        }
282 <        if (rt_pid == -1) {
283 <                perror(progname);
326 >        if (rval == 0) {
327 >                fprintf(stderr, "%s: command not found\n", av[0]);
328                  exit(1);
329          }
330 <        fd_tort = p0[1];
331 <        fd_fromrt = p1[0];
330 >        maxpix = rval/(6*sizeof(float));
331 >        if (maxpix > MAXPIX)
332 >                maxpix = MAXPIX;
333 >        maxpix--;
334   }
335  
336  
337   done_rtrace()                   /* wait for rtrace to finish */
338   {
339 <        int     pid, status;
339 >        int     status;
340  
341 <        if (rt_pid == -1)
342 <                return;
297 <        close(fd_tort);
298 <        close(fd_fromrt);
299 <        while ((pid = wait(&status)) != -1 && pid != rt_pid)
300 <                ;
301 <        if (pid == rt_pid && status != 0) {
341 >        status = close_process(rt_pd);
342 >        if (status > 0) {
343                  fprintf(stderr, "%s: bad status (%d) from rtrace\n",
344                                  progname, status);
345                  exit(1);
346          }
347 <        rt_pid = -1;
347 >        rt_pd[0] = -1;
348   }
349  
350  
351 < int
352 < readbuf(fd, bpos, siz)
312 < int     fd;
313 < char    *bpos;
314 < int     siz;
351 > SCAN *
352 > scanretire()                    /* retire old scanlines to free list */
353   {
354 <        register int    cc, nrem = siz;
355 <
356 <        while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
357 <                bpos += cc;
358 <                nrem -= cc;
354 >        SCAN    *sold[NRETIRE];
355 >        int     n;
356 >        int     h;
357 >        register SCAN   *sl;
358 >        register int    i;
359 >                                        /* grab the NRETIRE oldest scanlines */
360 >        sold[n = 0] = NULL;
361 >        for (h = 0; h < HSIZE; h++)
362 >                for (sl = hashtab[h]; sl != NULL; sl = sl->next) {
363 >                        for (i = n; i && sold[i-1]->lused > sl->lused; i--)
364 >                                if (i < NRETIRE)
365 >                                        sold[i] = sold[i-1];
366 >                        if (i < NRETIRE) {
367 >                                sold[i] = sl;
368 >                                if (n < NRETIRE)        /* grow list */
369 >                                        n++;
370 >                        }
371 >                }
372 >                                        /* put scanlines into free list */
373 >        for (i = 0; i < n; i++) {
374 >                h = shash(sold[i]->y);
375 >                sl = hashtab[h];
376 >                if (sl == sold[i])
377 >                        hashtab[h] = sl->next;
378 >                else {
379 >                        while (sl->next != sold[i])     /* IS in list */
380 >                                sl = sl->next;
381 >                        sl->next = sold[i]->next;
382 >                }
383 >                if (i > 0) {            /* save oldest as return value */
384 >                        sold[i]->next = freelist;
385 >                        freelist = sold[i];
386 >                }
387          }
388 <        if (cc < 0)
323 <                return(cc);
324 <        return(siz-nrem);
388 >        return(sold[0]);
389   }
390  
391  
392 < int
329 < writebuf(fd, bpos, siz)
330 < char    *bpos;
331 < int     siz;
332 < {
333 <        register int    cc, nrem = siz;
392 > static char     *scan_buf;
393  
394 <        while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
395 <                bpos += cc;
396 <                nrem -= cc;
394 >
395 > initscans()                             /* initialize scanline buffers */
396 > {
397 >        int     scansize;
398 >        register SCAN   *ptr;
399 >        register int    i;
400 >                                        /* initialize positions */
401 >        scanpos = (long *)bmalloc(pysiz*sizeof(long));
402 >        if (scanpos == NULL)
403 >                memerr("scanline positions");
404 >        for (i = pysiz-1; i >= 0; i--)
405 >                scanpos[i] = -1L;
406 >        curpos = pysiz-1;
407 >                                        /* clear hash table */
408 >        for (i = 0; i < HSIZE; i++)
409 >                hashtab[i] = NULL;
410 >                                        /* allocate scanline buffers */
411 >        scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
412 > #ifdef ALIGN
413 >        scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1);
414 > #endif
415 >        i = MAXSBUF / scansize;         /* compute number to allocate */
416 >        if (i > HSIZE)
417 >                i = HSIZE;
418 >        scanbufsiz = i*scansize;
419 >        scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */
420 >        if (scan_buf == NULL)
421 >                memerr("scanline buffers");
422 >        ptr = (SCAN *)scan_buf;
423 >        freelist = NULL;                /* build our free list */
424 >        while (i-- > 0) {
425 >                ptr->y = -1;
426 >                ptr->lused = -1;
427 >                ptr->next = freelist;
428 >                freelist = ptr;
429 >                ptr = (SCAN *)((char *)ptr + scansize); /* beware of C bugs */
430          }
431 <        if (cc < 0)
432 <                return(cc);
433 <        return(siz-nrem);
431 > }
432 >
433 >
434 > donescans()                             /* free up scanlines */
435 > {
436 >        bfree(scan_buf, scanbufsiz);
437 >        bfree((char *)scanpos, pysiz*sizeof(long));
438   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines