Back to index

radiance  4R0+20100331
Classes | Defines | Typedefs | Functions | Variables
glareval.c File Reference
#include "copyright.h"
#include <stdlib.h>
#include <string.h>
#include "rtprocess.h"
#include "glare.h"

Go to the source code of this file.

Classes

struct  scan

Defines

#define MAXPIX   (4096/(6*sizeof(float)))
#define MAXSBUF   786432 /* maximum total size of scanline buffer */
#define HSIZE   317 /* size of scanline hash table */
#define NRETIRE   16 /* number of scanlines to retire at once */
#define scandata(sl)   ((COLR *)((sl)+1))
#define shash(y)   ((y)%HSIZE)

Typedefs

typedef struct scan SCAN

Functions

static SCANclaimscan (int y)
static COLRgetpictscan (int y)
static double pict_val (FVECT vd)
static void rt_compute (float *pb, int np)
static SCANscanretire (void)
static void initscans (void)
static void donescans (void)
double getviewpix (int vh, int vv)
void getviewspan (int vv, float *vb)
static int getexpos (char *s, void *p)
void open_pict (char *fn)
void close_pict (void)
void fork_rtrace (char *av[])
void done_rtrace (void)

Variables

static const char RCSid [] = "$Id: glareval.c,v 2.12 2006/05/29 16:47:54 greg Exp $"
static SUBPROC rt_pd = SP_INACTIVE
FILE * pictfp = NULL
double exposure
int pxsiz
int pysiz
static int curpos
static long * scanpos
static int maxpix
static SCANfreelist
static SCANhashtab [HSIZE]
static long scanbufsiz
static long ncall = 0L
static long nread = 0L
static long nrecl = 0L
static int wrongformat = 0
static gethfunc getexpos
static char * scan_buf

Class Documentation

struct scan

Definition at line 32 of file glareval.c.

Collaboration diagram for scan:
Class Members
long lused
struct scan * next
int y

Define Documentation

#define HSIZE   317 /* size of scanline hash table */

Definition at line 20 of file glareval.c.

#define MAXPIX   (4096/(6*sizeof(float)))

Definition at line 17 of file glareval.c.

#define MAXSBUF   786432 /* maximum total size of scanline buffer */

Definition at line 19 of file glareval.c.

#define NRETIRE   16 /* number of scanlines to retire at once */

Definition at line 21 of file glareval.c.

#define scandata (   sl)    ((COLR *)((sl)+1))

Definition at line 39 of file glareval.c.

#define shash (   y)    ((y)%HSIZE)

Definition at line 40 of file glareval.c.


Typedef Documentation

typedef struct scan SCAN

Function Documentation

static SCAN * claimscan ( int  y) [static]

Definition at line 66 of file glareval.c.

{
       int    hi = shash(y);
       SCAN   *slast;
       register SCAN *sl;

       for (sl = hashtab[hi]; sl != NULL; sl = sl->next)
              if (sl->y == y)                           /* active scanline */
                     return(sl);
       for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next)
              if (sl->y == -1 || sl->y == y || sl->next == NULL) {
                     if (slast == NULL)          /* remove from free */
                            freelist = sl->next;
                     else
                            slast->next = sl->next;
                     if (sl->y == y) {           /* reclaim */
                            sl->next = hashtab[hi];
                            hashtab[hi] = sl;
                            nrecl++;
                     }
                     return(sl);
              }
       return(scanretire());              /* need more free scanlines */
}

Here is the call graph for this function:

Here is the caller graph for this function:

void close_pict ( void  )

Definition at line 333 of file glareval.c.

{
       if (pictfp == NULL)
              return;
       fclose(pictfp);
       donescans();
       pictfp = NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void done_rtrace ( void  )

Definition at line 367 of file glareval.c.

{
       int    status;

       status = close_process(&rt_pd);
       if (status > 0) {
              fprintf(stderr, "%s: bad status (%d) from rtrace\n",
                            progname, status);
              exit(1);
       }
       rt_pd.r = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void donescans ( void  ) [static]

Definition at line 466 of file glareval.c.

{
       bfree(scan_buf, scanbufsiz);
       bfree((char *)scanpos, pysiz*sizeof(long));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void fork_rtrace ( char *  av[])

Definition at line 344 of file glareval.c.

{
       int    rval;

       rval = open_process(&rt_pd, av);
       if (rval < 0) {
              perror(progname);
              exit(1);
       }
       if (rval == 0) {
              fprintf(stderr, "%s: command not found\n", av[0]);
              exit(1);
       }
       maxpix = rval/(6*sizeof(float));
       if (maxpix > MAXPIX)
              maxpix = MAXPIX;
       maxpix--;
}

Here is the call graph for this function:

static int getexpos ( char *  s,
void *  p 
) [static]

Definition at line 296 of file glareval.c.

{
       char   fmt[32];

       if (isexpos(s))
              exposure *= exposval(s);
       else if (isformat(s)) {
              formatval(fmt, s);
              wrongformat = strcmp(fmt, COLRFMT);
       }
       return(0);
}

Here is the call graph for this function:

static COLR * getpictscan ( int  y) [static]

Definition at line 95 of file glareval.c.

{
       register SCAN *sl;
       register int  i;
                                   /* first check our buffers */
       sl = claimscan(y);
       if (sl == NULL)
              memerr("claimscan()");
       sl->lused = ncall++;
       if (sl->y == y)                    /* scan hit */
              return(scandata(sl));
                                   /* else read in replacement */
       if (scanpos[y] < 0) {                     /* need to search */
              for (i = y+1; i < curpos; i++)
                     if (scanpos[i] >= 0) {
                            if (fseek(pictfp, scanpos[i], 0) < 0)
                                   goto seekerr;
                            curpos = i;
                            break;
                     }
              while (curpos >= y) {
                     scanpos[curpos] = ftell(pictfp);
                     if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
                            goto readerr;
                     nread++;
                     curpos--;
              }
       } else {
              if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0)
                     goto seekerr;
              if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0)
                     goto readerr;
              nread++;
              curpos = y-1;
       }
       sl->y = y;
       i = shash(y);               /* add to hash list */
       sl->next = hashtab[i];
       hashtab[i] = sl;
       return(scandata(sl));
readerr:
       fprintf(stderr, "%s: picture read error\n", progname);
       exit(1);
seekerr:
       fprintf(stderr, "%s: picture seek error\n", progname);
       exit(1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

double getviewpix ( int  vh,
int  vv 
)

Definition at line 189 of file glareval.c.

{
       FVECT  dir;
       float  rt_buf[12];
       double res;

       if (compdir(dir, vh, vv) < 0)
              return(-1.0);
       npixinvw++;
       if ((res = pict_val(dir)) >= 0.0)
              return(res);
       if (rt_pd.r == -1) {
              npixmiss++;
              return(-1.0);
       }
       rt_buf[0] = ourview.vp[0];
       rt_buf[1] = ourview.vp[1];
       rt_buf[2] = ourview.vp[2];
       rt_buf[3] = dir[0];
       rt_buf[4] = dir[1];
       rt_buf[5] = dir[2];
       rt_compute(rt_buf, 1);
       return(luminance(rt_buf));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void getviewspan ( int  vv,
float *  vb 
)

Definition at line 219 of file glareval.c.

{
       float  rt_buf[6*MAXPIX];    /* rtrace send/receive buffer */
       register int  n;            /* number of pixels in buffer */
       short  buf_vh[MAXPIX];             /* pixel positions */
       FVECT  dir;
       register int  vh;

#ifdef DEBUG
       if (verbose)
              fprintf(stderr, "%s: computing view span at %d...\n",
                            progname, vv);
#endif
       n = 0;
       for (vh = -hsize; vh <= hsize; vh++) {
              if (compdir(dir, vh, vv) < 0) {           /* not in view */
                     vb[vh+hsize] = -1.0;
                     continue;
              }
              npixinvw++;
              if ((vb[vh+hsize] = pict_val(dir)) >= 0.0)
                     continue;
              if (rt_pd.r == -1) {        /* missing information */
                     npixmiss++;
                     continue;
              }
                                          /* send to rtrace */
              if (n >= maxpix) {                 /* flush */
                     rt_compute(rt_buf, n);
                     while (n-- > 0)
                            vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
              }
              rt_buf[6*n] = ourview.vp[0];
              rt_buf[6*n+1] = ourview.vp[1];
              rt_buf[6*n+2] = ourview.vp[2];
              rt_buf[6*n+3] = dir[0];
              rt_buf[6*n+4] = dir[1];
              rt_buf[6*n+5] = dir[2];
              buf_vh[n++] = vh;
       }
#ifdef DEBUG
       if (verbose)
              pict_stats();
#endif
       if (n > 0) {                       /* process pending buffer */
              rt_compute(rt_buf, n);
              while (n-- > 0)
                     vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void initscans ( void  ) [static]

Definition at line 426 of file glareval.c.

{
       int    scansize;
       register SCAN *ptr;
       register int  i;
                                   /* initialize positions */
       scanpos = (long *)bmalloc(pysiz*sizeof(long));
       if (scanpos == NULL)
              memerr("scanline positions");
       for (i = pysiz-1; i >= 0; i--)
              scanpos[i] = -1L;
       curpos = pysiz-1;
                                   /* clear hash table */
       for (i = 0; i < HSIZE; i++)
              hashtab[i] = NULL;
                                   /* allocate scanline buffers */
       scansize = sizeof(SCAN) + pxsiz*sizeof(COLR);
#ifdef ALIGNT
       scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1);
#endif
       i = MAXSBUF / scansize;            /* compute number to allocate */
       if (i > HSIZE)
              i = HSIZE;
       scanbufsiz = i*scansize;
       scan_buf = bmalloc(scanbufsiz);    /* get in one big chunk */
       if (scan_buf == NULL)
              memerr("scanline buffers");
       ptr = (SCAN *)scan_buf;
       freelist = NULL;            /* build our free list */
       while (i-- > 0) {
              ptr->y = -1;
              ptr->lused = -1;
              ptr->next = freelist;
              freelist = ptr;
              ptr = (SCAN *)((char *)ptr + scansize);   /* beware of C bugs */
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void open_pict ( char *  fn)

Definition at line 314 of file glareval.c.

{
       if ((pictfp = fopen(fn, "r")) == NULL) {
              fprintf(stderr, "%s: cannot open\n", fn);
              exit(1);
       }
       exposure = 1.0;
       getheader(pictfp, getexpos, NULL);
       if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) {
              fprintf(stderr, "%s: incompatible picture format\n", fn);
              exit(1);
       }
       initscans();
}

Here is the call graph for this function:

static double pict_val ( FVECT  vd) [static]

Definition at line 166 of file glareval.c.

{
       FVECT  pp;
       FVECT  ip;
       COLOR  res;

       if (pictfp == NULL)
              return(-1.0);
       pp[0] = pictview.vp[0] + vd[0];
       pp[1] = pictview.vp[1] + vd[1];
       pp[2] = pictview.vp[2] + vd[2];
       viewloc(ip, &pictview, pp);
       if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. ||
                     ip[1] < 0. || ip[1] >= 1.)
              return(-1.0);
       colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]);
       return(luminance(res)/exposure);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void rt_compute ( float *  pb,
int  np 
) [static]

Definition at line 275 of file glareval.c.

{
#ifdef DEBUG
       if (verbose && np > 1)
              fprintf(stderr, "%s: sending %d samples to rtrace...\n",
                            progname, np);
#endif
       memset(pb+6*np, '\0', 6*sizeof(float));
       if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1),
                     6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) {
              fprintf(stderr, "%s: rtrace communication error\n",
                            progname);
              exit(1);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static SCAN * scanretire ( void  ) [static]

Definition at line 382 of file glareval.c.

{
       SCAN   *sold[NRETIRE];
       int    n;
       int    h;
       register SCAN *sl;
       register int  i;
                                   /* grab the NRETIRE oldest scanlines */
       sold[n = 0] = NULL;
       for (h = 0; h < HSIZE; h++)
              for (sl = hashtab[h]; sl != NULL; sl = sl->next) {
                     for (i = n; i && sold[i-1]->lused > sl->lused; i--)
                            if (i < NRETIRE)
                                   sold[i] = sold[i-1];
                     if (i < NRETIRE) {
                            sold[i] = sl;
                            if (n < NRETIRE)     /* grow list */
                                   n++;
                     }
              }
                                   /* put scanlines into free list */
       for (i = 0; i < n; i++) {
              h = shash(sold[i]->y);
              sl = hashtab[h];
              if (sl == sold[i])
                     hashtab[h] = sl->next;
              else {
                     while (sl->next != sold[i]) /* IS in list */
                            sl = sl->next;
                     sl->next = sold[i]->next;
              }
              if (i > 0) {         /* save oldest as return value */
                     sold[i]->next = freelist;
                     freelist = sold[i];
              }
       }
       return(sold[0]);
}

Here is the caller graph for this function:


Variable Documentation

int curpos [static]

Definition at line 29 of file glareval.c.

double exposure

Definition at line 26 of file glareval.c.

SCAN* freelist [static]

Definition at line 44 of file glareval.c.

gethfunc getexpos [static]

Definition at line 59 of file glareval.c.

SCAN* hashtab[HSIZE] [static]

Definition at line 45 of file glareval.c.

int maxpix [static]

Definition at line 42 of file glareval.c.

long ncall = 0L [static]

Definition at line 49 of file glareval.c.

long nread = 0L [static]

Definition at line 50 of file glareval.c.

long nrecl = 0L [static]

Definition at line 51 of file glareval.c.

FILE* pictfp = NULL

Definition at line 25 of file glareval.c.

int pxsiz

Definition at line 27 of file glareval.c.

int pysiz

Definition at line 27 of file glareval.c.

const char RCSid[] = "$Id: glareval.c,v 2.12 2006/05/29 16:47:54 greg Exp $" [static]

Definition at line 2 of file glareval.c.

Definition at line 23 of file glareval.c.

char* scan_buf [static]

Definition at line 422 of file glareval.c.

long scanbufsiz [static]

Definition at line 47 of file glareval.c.

long* scanpos [static]

Definition at line 30 of file glareval.c.

int wrongformat = 0 [static]

Definition at line 53 of file glareval.c.