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.2 by greg, Mon Mar 18 14:33:53 1991 UTC vs.
Revision 1.14 by greg, Fri Apr 19 17:41:06 1991 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20   #define vfork           fork
21   #endif
22  
23 < #define NSCANS          32              /* number of scanlines to buffer */
23 > #define MAXSBUF         524268  /* maximum total size of scanline buffer */
24 > #define HSIZE           317     /* size of scanline hash table */
25 > #define NRETIRE         16      /* number of scanlines to retire at once */
26  
27   int     rt_pid = -1;            /* process id for rtrace */
28   int     fd_tort, fd_fromrt;     /* pipe descriptors */
# Line 28 | Line 30 | int    fd_tort, fd_fromrt;     /* pipe descriptors */
30   FILE    *pictfp = NULL;         /* picture file pointer */
31   double  exposure;               /* picture exposure */
32   int     pxsiz, pysiz;           /* picture dimensions */
33 < int     curpos;                 /* current scanline */
34 < long    *scanpos;               /* scanline positions */
35 < struct {
36 <        long    lused;          /* for LRU replacement */
33 >
34 > static int      curpos;         /* current scanline */
35 > static long     *scanpos;       /* scanline positions */
36 >
37 > typedef struct scan {
38          int     y;              /* scanline position */
39 <        COLR    *sl;            /* scanline contents */
40 < } scan[NSCANS];         /* buffered scanlines */
39 >        long    lused;          /* for LRU replacement */
40 >        struct scan     *next;  /* next in this hash or free list */
41 >        /* followed by the scanline data */
42 > } SCAN;                 /* buffered scanline */
43  
44 + #define scandata(sl)    ((COLR *)((sl)+1))
45 + #define shash(y)        ((y)%HSIZE)
46 +
47 + static SCAN     *freelist;              /* scanline free list */
48 + static SCAN     *hashtab[HSIZE];        /* scanline hash table */
49 +
50   static long     ncall = 0L;     /* number of calls to getpictscan */
51   static long     nread = 0L;     /* number of scanlines read */
52  
53 + static int      wrongformat = 0;
54  
55 + SCAN    *scanretire();
56 +
57 + extern long     ftell();
58 +
59 +
60 + SCAN *
61 + claimscan(y)                    /* claim scanline from buffers */
62 + int     y;
63 + {
64 +        int     hi = shash(y);
65 +        SCAN    *slast;
66 +        register SCAN   *sl;
67 +
68 +        for (sl = hashtab[hi]; sl != NULL; sl = sl->next)
69 +                if (sl->y == y)                         /* active scanline */
70 +                        return(sl);
71 +        for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next)
72 +                if (sl->y == -1 || sl->y == y || sl->next == NULL) {
73 +                        if (slast == NULL)              /* remove from free */
74 +                                freelist = sl->next;
75 +                        else
76 +                                slast->next = sl->next;
77 +                        if (sl->y == y) {               /* reclaim */
78 +                                sl->next = hashtab[hi];
79 +                                hashtab[hi] = sl;
80 + #ifdef DEBUG
81 +                                if (verbose)
82 +                                        fprintf(stderr,
83 +                                                "%s: scanline %d reclaimed\n",
84 +                                                        progname, y);
85 + #endif
86 +                        }
87 +                        return(sl);
88 +                }
89 +        return(scanretire());           /* need more free scanlines */
90 + }
91 +
92 +
93   COLR *
94   getpictscan(y)                  /* get picture scanline */
95   int     y;
96   {
97 <        extern long     ftell();
48 <        int     minused;
97 >        register SCAN   *sl;
98          register int    i;
99                                          /* first check our buffers */
100 <        ncall++;
101 <        minused = 0;
102 <        for (i = 0; i < NSCANS; i++) {
103 <                if (scan[i].y == y) {
104 <                        scan[i].lused = ncall;
105 <                        return(scan[i].sl);
106 <                }
107 <                if (scan[i].lused < scan[minused].lused)
108 <                        minused = i;
109 <        }
110 <                                        /* not there, read it in */
111 <        if (scanpos[y] == -1) {                 /* need to search */
112 <                while (curpos > y) {
100 >        sl = claimscan(y);
101 >        if (sl == NULL)
102 >                memerr("claimscan()");
103 >        sl->lused = ncall++;
104 >        if (sl->y == y)                 /* scan hit */
105 >                return(scandata(sl));
106 >                                        /* else read in replacement */
107 >        if (scanpos[y] < 0) {                   /* need to search */
108 >                for (i = y+1; i < curpos; i++)
109 >                        if (scanpos[i] >= 0) {
110 >                                if (fseek(pictfp, scanpos[i], 0) < 0)
111 >                                        goto seekerr;
112 >                                curpos = i;
113 >                                break;
114 >                        }
115 >                while (curpos >= y) {
116                          scanpos[curpos] = ftell(pictfp);
117 <                        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
117 >                        if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
118                                  goto readerr;
119 +                        nread++;
120                          curpos--;
121                  }
122 <        } else if (fseek(pictfp, scanpos[y], 0) < 0) {
123 <                fprintf(stderr, "%s: picture seek error\n", progname);
124 <                exit(1);
122 >        } else {
123 >                if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
124 >                        goto seekerr;
125 >                if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
126 >                        goto readerr;
127 >                nread++;
128 >                curpos = y-1;
129          }
130 <        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
131 <                goto readerr;
132 <        nread++;
133 <        curpos = y-1;
134 <        scan[minused].lused = ncall;
78 <        scan[minused].y = y;
79 <        return(scan[minused].sl);
130 >        sl->y = y;
131 >        i = shash(y);                   /* add to hash list */
132 >        sl->next = hashtab[i];
133 >        hashtab[i] = sl;
134 >        return(scandata(sl));
135   readerr:
136          fprintf(stderr, "%s: picture read error\n", progname);
137          exit(1);
138 + seekerr:
139 +        fprintf(stderr, "%s: picture seek error\n", progname);
140 +        exit(1);
141   }
142  
143  
144 + #ifdef DEBUG
145   pict_stats()                    /* print out picture read statistics */
146   {
147          static long     lastcall = 0L;  /* ncall at last report */
# Line 90 | Line 149 | pict_stats()                   /* print out picture read statistics */
149  
150          if (ncall == lastcall)
151                  return;
152 <        fprintf(stderr, "%s: %ld scanlines read, %ld reused\n",
153 <                        progname, nread-lastread,
95 <                        (ncall-lastcall)-(nread-lastread));
152 >        fprintf(stderr, "%s: %ld scanlines read in %ld calls\n",
153 >                        progname, nread-lastread, ncall-lastcall);
154          lastcall = ncall;
155          lastread = nread;
156   }
157 + #endif
158  
159  
160   double
# Line 129 | Line 188 | int    vh, vv;
188  
189          if (compdir(dir, vh, vv) < 0)
190                  return(-1.0);
191 +        npixinvw++;
192          if ((res = pict_val(dir)) >= 0.0)
193                  return(res);
194 <        if (rt_pid == -1)
194 >        if (rt_pid == -1) {
195 >                npixmiss++;
196                  return(-1.0);
197 +        }
198          rt_buf[0] = ourview.vp[0];
199          rt_buf[1] = ourview.vp[1];
200          rt_buf[2] = ourview.vp[2];
# Line 149 | Line 211 | int    vv;
211   float   *vb;
212   {
213          float   rt_buf[6*MAXPIX];       /* rtrace send/receive buffer */
214 <        int     npix_tort;              /* number of pixels in buffer */
214 >        register int    n;              /* number of pixels in buffer */
215          short   buf_vh[MAXPIX];         /* pixel positions */
216          FVECT   dir;
217          register int    vh;
156        register int    i;
218  
219 + #ifdef DEBUG
220          if (verbose)
221                  fprintf(stderr, "%s: computing view span at %d...\n",
222                                  progname, vv);
223 <        npix_tort = 0;
223 > #endif
224 >        n = 0;
225          for (vh = -hsize; vh <= hsize; vh++) {
226 <                if (compdir(dir, vh, vv) < 0) { /* off viewable region */
226 >                if (compdir(dir, vh, vv) < 0) {         /* not in view */
227                          vb[vh+hsize] = -1.0;
228                          continue;
229                  }
230 +                npixinvw++;
231                  if ((vb[vh+hsize] = pict_val(dir)) >= 0.0)
232                          continue;
233 <                if (rt_pid == -1)               /* missing information */
233 >                if (rt_pid == -1) {             /* missing information */
234 >                        npixmiss++;
235                          continue;
236 +                }
237                                                  /* send to rtrace */
238 <                if (npix_tort >= MAXPIX) {              /* flush */
239 <                        rt_compute(rt_buf, npix_tort);
240 <                        for (i = 0; i < npix_tort; i++)
241 <                                vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
176 <                        npix_tort = 0;
238 >                if (n >= MAXPIX) {                      /* flush */
239 >                        rt_compute(rt_buf, n);
240 >                        while (n-- > 0)
241 >                                vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
242                  }
243 <                rt_buf[6*npix_tort] = ourview.vp[0];
244 <                rt_buf[6*npix_tort+1] = ourview.vp[1];
245 <                rt_buf[6*npix_tort+2] = ourview.vp[2];
246 <                rt_buf[6*npix_tort+3] = dir[0];
247 <                rt_buf[6*npix_tort+4] = dir[1];
248 <                rt_buf[6*npix_tort+5] = dir[2];
249 <                buf_vh[npix_tort++] = vh;
243 >                rt_buf[6*n] = ourview.vp[0];
244 >                rt_buf[6*n+1] = ourview.vp[1];
245 >                rt_buf[6*n+2] = ourview.vp[2];
246 >                rt_buf[6*n+3] = dir[0];
247 >                rt_buf[6*n+4] = dir[1];
248 >                rt_buf[6*n+5] = dir[2];
249 >                buf_vh[n++] = vh;
250          }
251 <        if (npix_tort > 0) {                    /* process pending buffer */
187 <                rt_compute(rt_buf, npix_tort);
188 <                for (i = 0; i < npix_tort; i++)
189 <                        vb[buf_vh[i]+hsize] = luminance(rt_buf+3*i);
190 <        }
251 > #ifdef DEBUG
252          if (verbose)
253                  pict_stats();
254 + #endif
255 +        if (n > 0) {                            /* process pending buffer */
256 +                rt_compute(rt_buf, n);
257 +                while (n-- > 0)
258 +                        vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
259 +        }
260   }
261  
262  
# Line 199 | Line 266 | int    np;
266   {
267          static float    nbuf[6] = {0.,0.,0.,0.,0.,0.};
268  
269 + #ifdef DEBUG
270          if (verbose && np > 1)
271                  fprintf(stderr, "%s: sending %d samples to rtrace...\n",
272                                  progname, np);
273 + #endif
274          if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np
275                  || writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) {
276                  fprintf(stderr, "%s: error writing to rtrace process\n",
# Line 220 | Line 289 | int    np;
289   getexpos(s)                     /* get exposure from header line */
290   char    *s;
291   {
292 +        char    fmt[32];
293 +
294          if (isexpos(s))
295                  exposure *= exposval(s);
296 +        else if (isformat(s)) {
297 +                formatval(fmt, s);
298 +                wrongformat = strcmp(fmt, COLRFMT);
299 +        }
300   }
301  
302  
303   open_pict(fn)                   /* open picture file */
304   char    *fn;
305   {
231        register int    i;
232
306          if ((pictfp = fopen(fn, "r")) == NULL) {
307                  fprintf("%s: cannot open\n", fn);
308                  exit(1);
309          }
310          exposure = 1.0;
311 <        getheader(pictfp, getexpos);
312 <        if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
313 <                fprintf("%s: bad picture resolution\n", fn);
311 >        getheader(pictfp, getexpos, NULL);
312 >        if (wrongformat ||
313 >                        fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) {
314 >                fprintf("%s: bad picture format\n", fn);
315                  exit(1);
316          }
317 <        scanpos = (long *)malloc(pysiz*sizeof(long));
244 <        if (scanpos == NULL)
245 <                memerr("scanline positions");
246 <        for (i = 0; i < pysiz; i++)
247 <                scanpos[i] = -1L;
248 <        for (i = 0; i < NSCANS; i++) {
249 <                scan[i].lused = -1;
250 <                scan[i].y = -1;
251 <                scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR));
252 <                if (scan[i].sl == NULL)
253 <                        memerr("scanline buffers");
254 <        }
255 <        curpos = pysiz-1;
317 >        initscans();
318   }
319  
320  
321   close_pict()                    /* done with picture */
322   {
261        register int    i;
262
323          if (pictfp == NULL)
324                  return;
325          fclose(pictfp);
326 <        free((char *)scanpos);
267 <        for (i = 0; i < NSCANS; i++)
268 <                free((char *)scan[i].sl);
326 >        donescans();
327          pictfp = NULL;
328   }
329  
# Line 298 | Line 356 | char   *av[];
356                  perror(progname);
357                  exit(1);
358          }
359 +        close(p0[0]);
360 +        close(p1[1]);
361          fd_tort = p0[1];
362          fd_fromrt = p1[0];
363   }
# Line 354 | Line 414 | int    siz;
414          if (cc < 0)
415                  return(cc);
416          return(siz-nrem);
417 + }
418 +
419 +
420 + SCAN *
421 + scanretire()                    /* retire old scanlines to free list */
422 + {
423 +        SCAN    *sold[NRETIRE];
424 +        int     n;
425 +        int     h;
426 +        register SCAN   *sl;
427 +        register int    i;
428 +                                        /* grab the NRETIRE oldest scanlines */
429 +        sold[n = 0] = NULL;
430 +        for (h = 0; h < HSIZE; h++)
431 +                for (sl = hashtab[h]; sl != NULL; sl = sl->next) {
432 +                        for (i = n; i && sold[i-1]->lused > sl->lused; i--)
433 +                                if (i < NRETIRE)
434 +                                        sold[i] = sold[i-1];
435 +                        if (i < NRETIRE) {
436 +                                sold[i] = sl;
437 +                                if (n < NRETIRE)        /* grow list */
438 +                                        n++;
439 +                        }
440 +                }
441 +                                        /* put scanlines into free list */
442 +        for (i = 0; i < n; i++) {
443 +                h = shash(sold[i]->y);
444 +                sl = hashtab[h];
445 +                if (sl == sold[i])
446 +                        hashtab[h] = sl->next;
447 +                else {
448 +                        while (sl->next != sold[i])     /* IS in list */
449 +                                sl = sl->next;
450 +                        sl->next = sold[i]->next;
451 +                }
452 +                if (i > 0) {            /* save oldest as return value */
453 +                        sold[i]->next = freelist;
454 +                        freelist = sold[i];
455 +                }
456 +        }
457 +        return(sold[0]);
458 + }
459 +
460 +
461 + static char     *scan_buf;
462 +
463 +
464 + initscans()                             /* initialize scanline buffers */
465 + {
466 +        int     scansize;
467 +        register SCAN   *ptr;
468 +        register int    i;
469 +                                        /* initialize positions */
470 +        scanpos = (long *)malloc(pysiz*sizeof(long));
471 +        if (scanpos == NULL)
472 +                memerr("scanline positions");
473 +        for (i = pysiz-1; i >= 0; i--)
474 +                scanpos[i] = -1L;
475 +        curpos = pysiz-1;
476 +                                        /* clear hash table */
477 +        for (i = 0; i < HSIZE; i++)
478 +                hashtab[i] = NULL;
479 +                                        /* allocate scanline buffers */
480 +        scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
481 + #ifdef ALIGN
482 +        scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1);
483 + #endif
484 +        i = MAXSBUF / scansize;         /* compute number to allocate */
485 +        if (i > HSIZE)
486 +                i = HSIZE;
487 +        scan_buf = malloc(i*scansize);  /* get in one big chunk */
488 +        if (scan_buf == NULL)
489 +                memerr("scanline buffers");
490 +        ptr = (SCAN *)scan_buf;
491 +        freelist = NULL;                /* build our free list */
492 +        while (i-- > 0) {
493 +                ptr->y = -1;
494 +                ptr->lused = -1;
495 +                ptr->next = freelist;
496 +                freelist = ptr;
497 +                ptr = (SCAN *)((char *)ptr + scansize); /* beware of C bugs */
498 +        }
499 + }
500 +
501 +
502 + donescans()                             /* free up scanlines */
503 + {
504 +        free(scan_buf);
505 +        free((char *)scanpos);
506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines