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.6 by greg, Thu Mar 21 16:46:25 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          64              /* 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 <        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 <                }
59 <                if (scan[i].lused < scan[minused].lused)
60 <                        minused = i;
61 <        }
62 <                                        /* not there, read it in */
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) {
# Line 70 | Line 114 | int    y;
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--;
# Line 78 | Line 122 | int    y;
122          } else {
123                  if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
124                          goto seekerr;
125 <                if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
125 >                if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
126                          goto readerr;
127                  nread++;
128                  curpos = y-1;
129          }
130 <        scan[minused].lused = ncall;
131 <        scan[minused].y = y;
132 <        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);
# Line 142 | 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 174 | Line 223 | float  *vb;
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 (n >= MAXPIX) {                      /* flush */
239                          rt_compute(rt_buf, n);
# Line 237 | 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   {
248        register int    i;
249
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));
261 <        if (scanpos == NULL)
262 <                memerr("scanline positions");
263 <        for (i = pysiz-1; i >= 0; i--)
264 <                scanpos[i] = -1L;
265 <        curpos = pysiz-1;
266 <        for (i = 0; i < NSCANS; i++) {
267 <                scan[i].lused = -1;
268 <                scan[i].y = -1;
269 <                scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR));
270 <                if (scan[i].sl == NULL)
271 <                        memerr("scanline buffers");
272 <        }
317 >        initscans();
318   }
319  
320  
321   close_pict()                    /* done with picture */
322   {
278        register int    i;
279
323          if (pictfp == NULL)
324                  return;
325          fclose(pictfp);
326 <        free((char *)scanpos);
284 <        for (i = 0; i < NSCANS; i++)
285 <                free((char *)scan[i].sl);
326 >        donescans();
327          pictfp = NULL;
328   }
329  
# Line 373 | 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