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.7 by greg, Tue Apr 2 14:29:17 1991 UTC vs.
Revision 1.8 by greg, Wed Apr 3 15:20:35 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         409580  /* maximum total size of scanline buffer */
24 > #define HSIZE           227     /* 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 + SCAN    *scanretire();
54 +
55   extern long     ftell();
56  
57  
58 + SCAN *
59 + claimscan(y)                    /* claim scanline from buffers */
60 + int     y;
61 + {
62 +        int     hi = shash(y);
63 +        SCAN    *slast;
64 +        register SCAN   *sl;
65 +
66 +        for (sl = hashtab[hi]; sl != NULL; sl = sl->next)
67 +                if (sl->y == y)                         /* active scanline */
68 +                        return(sl);
69 +        for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next)
70 +                if (sl->y == -1 || sl->y == y || sl->next == NULL) {
71 +                        if (slast == NULL)              /* remove from free */
72 +                                freelist = sl->next;
73 +                        else
74 +                                slast->next = sl->next;
75 +                        if (sl->y == y) {               /* reclaim */
76 +                                sl->next = hashtab[hi];
77 +                                hashtab[hi] = sl;
78 +                        }
79 +                        return(sl);
80 +                }
81 +        return(scanretire());           /* need more free scanlines */
82 + }
83 +
84 +
85   COLR *
86   getpictscan(y)                  /* get picture scanline */
87   int     y;
88   {
89 <        int     minused;
89 >        register SCAN   *sl;
90          register int    i;
91                                          /* first check our buffers */
92 <        ncall++;
93 <        minused = 0;
94 <        for (i = 0; i < NSCANS; i++) {
95 <                if (scan[i].y == y) {
96 <                        scan[i].lused = ncall;
97 <                        return(scan[i].sl);
98 <                }
59 <                if (scan[i].lused < scan[minused].lused)
60 <                        minused = i;
61 <        }
62 <                                        /* not there, read it in */
92 >        sl = claimscan(y);
93 >        if (sl == NULL)
94 >                memerr("claimscan()");
95 >        sl->lused = ncall++;
96 >        if (sl->y == y)                 /* scan hit */
97 >                return(scandata(sl));
98 >                                        /* else read in replacement */
99          if (scanpos[y] < 0) {                   /* need to search */
100                  for (i = y+1; i < curpos; i++)
101                          if (scanpos[i] >= 0) {
# Line 70 | Line 106 | int    y;
106                          }
107                  while (curpos >= y) {
108                          scanpos[curpos] = ftell(pictfp);
109 <                        if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
109 >                        if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
110                                  goto readerr;
111                          nread++;
112                          curpos--;
# Line 78 | Line 114 | int    y;
114          } else {
115                  if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
116                          goto seekerr;
117 <                if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0)
117 >                if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
118                          goto readerr;
119                  nread++;
120                  curpos = y-1;
121          }
122 <        scan[minused].lused = ncall;
123 <        scan[minused].y = y;
124 <        return(scan[minused].sl);
122 >        sl->y = y;
123 >        i = shash(y);                   /* add to hash list */
124 >        sl->next = hashtab[i];
125 >        hashtab[i] = sl;
126 >        return(scandata(sl));
127   readerr:
128          fprintf(stderr, "%s: picture read error\n", progname);
129          exit(1);
# Line 263 | Line 301 | char   *fn;
301                  fprintf("%s: bad picture resolution\n", fn);
302                  exit(1);
303          }
304 <        scanpos = (long *)malloc(pysiz*sizeof(long));
267 <        if (scanpos == NULL)
268 <                memerr("scanline positions");
269 <        for (i = pysiz-1; i >= 0; i--)
270 <                scanpos[i] = -1L;
271 <        curpos = pysiz-1;
272 <        for (i = 0; i < NSCANS; i++) {
273 <                scan[i].lused = -1;
274 <                scan[i].y = -1;
275 <                scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR));
276 <                if (scan[i].sl == NULL)
277 <                        memerr("scanline buffers");
278 <        }
304 >        initscans();
305   }
306  
307  
# Line 286 | Line 312 | close_pict()                   /* done with picture */
312          if (pictfp == NULL)
313                  return;
314          fclose(pictfp);
315 <        free((char *)scanpos);
290 <        for (i = 0; i < NSCANS; i++)
291 <                free((char *)scan[i].sl);
315 >        donescans();
316          pictfp = NULL;
317   }
318  
# Line 379 | Line 403 | int    siz;
403          if (cc < 0)
404                  return(cc);
405          return(siz-nrem);
406 + }
407 +
408 +
409 + SCAN *
410 + scanretire()                    /* retire old scanlines to free list */
411 + {
412 +        SCAN    *sold[NRETIRE];
413 +        SCAN    head;
414 +        int     n;
415 +        int     h;
416 +        register SCAN   *sl;
417 +        register int    i;
418 +                                        /* grab the NRETIRE oldest scanlines */
419 +        sold[n = 0] = NULL;
420 +        for (h = 0; h < HSIZE; h++) {
421 +                head.next = hashtab[h];
422 +                sl = &head;
423 +                while (sl->next != NULL) {
424 +                        for (i = n; i && sold[i-1]->lused > sl->next->lused; i--)
425 +                                if (i == NRETIRE) {     /* reallocate */
426 +                                        register int    oh;
427 +                                        oh = shash(sold[NRETIRE-1]->y);
428 +                                        sold[NRETIRE-1]->next = hashtab[oh];
429 +                                        if (h == oh && sl == &head)
430 +                                                head.next = sl = sold[NRETIRE-1];
431 +                                        else
432 +                                                hashtab[oh] = sold[NRETIRE-1];
433 +                                } else                  /* else bubble up */
434 +                                        sold[i] = sold[i-1];
435 +                        if (i < NRETIRE) {
436 +                                sold[i] = sl->next;
437 +                                sl->next = sl->next->next;
438 +                                if (n < NRETIRE)        /* grow list */
439 +                                        n++;
440 +                        } else
441 +                                sl = sl->next;
442 +                }
443 +                hashtab[h] = head.next;
444 +        }
445 +                                        /* put scanlines into free list */
446 +        for (i = 1; i < n; i++) {
447 +                sold[i]->next = freelist;
448 +                freelist = sold[i];
449 +        }
450 +        return(sold[0]);
451 + }
452 +
453 +
454 + static char     *scan_buf;
455 +
456 +
457 + initscans()                             /* initialize scanline buffers */
458 + {
459 +        int     scansize;
460 +        register SCAN   *ptr;
461 +        register int    i;
462 +                                        /* initialize positions */
463 +        scanpos = (long *)malloc(pysiz*sizeof(long));
464 +        if (scanpos == NULL)
465 +                memerr("scanline positions");
466 +        for (i = pysiz-1; i >= 0; i--)
467 +                scanpos[i] = -1L;
468 +        curpos = pysiz-1;
469 +                                        /* clear hash table */
470 +        for (i = 0; i < HSIZE; i++)
471 +                hashtab[i] = NULL;
472 +                                        /* allocate scanline buffers */
473 +        scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
474 + #ifdef ALIGN
475 +        scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1);
476 + #endif
477 +        i = MAXSBUF / scansize;         /* compute number to allocate */
478 +        if (i > HSIZE)
479 +                i = HSIZE;
480 +        scan_buf = malloc(i*scansize);  /* get in one big chunk */
481 +        if (scan_buf == NULL)
482 +                memerr("scanline buffers");
483 +        ptr = (SCAN *)scan_buf;
484 +        freelist = NULL;                /* build our free list */
485 +        while (i-- > 0) {
486 +                ptr->y = -1;
487 +                ptr->lused = -1;
488 +                ptr->next = freelist;
489 +                freelist = ptr;
490 +                ptr = (SCAN *)((char *)ptr + scansize); /* beware of C bugs */
491 +        }
492 + }
493 +
494 +
495 + donescans()                             /* free up scanlines */
496 + {
497 +        free(scan_buf);
498 +        free((char *)scanpos);
499   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines