--- ray/src/util/rtcontrib.c 2010/06/11 20:26:09 1.56 +++ ray/src/util/rtcontrib.c 2011/10/05 17:20:55 1.65 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.56 2010/06/11 20:26:09 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.65 2011/10/05 17:20:55 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -16,11 +16,15 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.56 2 #include "lookup.h" #include "calcomp.h" +#ifdef _WIN32 +typedef long ssize_t; +#endif + #ifndef MAXMODLIST #define MAXMODLIST 1024 /* maximum modifiers we'll track */ #endif -int treebufsiz = BUFSIZ; /* current tree buffer size */ +ssize_t treebufsiz = BUFSIZ; /* current tree buffer size */ typedef double DCOLOR[3]; /* double-precision color */ @@ -92,9 +96,9 @@ struct rtproc { struct rtproc *next; /* next in list of processes */ SUBPROC pd; /* rtrace pipe descriptors */ unsigned long raynum; /* ray number for this tree */ - int bsiz; /* ray tree buffer length */ + size_t bsiz; /* ray tree buffer length */ char *buf; /* ray tree buffer */ - int nbr; /* number of bytes from rtrace */ + size_t nbr; /* number of bytes from rtrace */ }; /* rtrace process buffer */ /* rtrace command and defaults */ @@ -107,8 +111,8 @@ int rtargc = 9; char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", "-dt", "0", "-as", "0", "-aa", "0", NULL }; -#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ -#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ +#define RTCOEFF "-o~~~TmWdp" /* compute coefficients only */ +#define RTCONTRIB "-o~~~TmVdp" /* compute ray contributions */ struct rtproc rt0; /* head of rtrace process list */ @@ -545,7 +549,7 @@ init(int np) raysleft = 0; if ((account = accumulate) > 0) raysleft *= accumulate; - waitflush = xres; + waitflush = (yres > 0) & (xres > 1) ? 0 : xres; if (!recover) return; /* recover previous values */ @@ -702,12 +706,19 @@ void printheader(FILE *fout, const char *info) { extern char VersionID[]; - FILE *fin = fopen(octree, "r"); - - if (fin == NULL) - quit(1); - checkheader(fin, "ignore", fout); /* copy octree header */ - fclose(fin); + /* copy octree header */ + if (octree[0] == '!') { + newheader("RADIANCE", fout); + fputs(octree+1, fout); + if (octree[strlen(octree)-1] != '\n') + fputc('\n', fout); + } else { + FILE *fin = fopen(octree, "r"); + if (fin == NULL) + quit(1); + checkheader(fin, "ignore", fout); + fclose(fin); + } printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); @@ -736,8 +747,6 @@ printresolu(FILE *fout, int xr, int yr) { if ((xr > 0) & (yr > 0)) /* resolution string */ fprtresolu(xr, yr, fout); - if (xres > 0) /* global flush flag */ - fflush(fout); } /* Get output stream pointer (open and write header if new and noopen==0) */ @@ -758,6 +767,8 @@ getostream(const char *ospec, const char *mname, int b if (header) printheader(stdout, NULL); printresolu(stdout, xres, yres); + if (waitflush > 0) + fflush(stdout); stdos.xr = xres; stdos.yr = yres; using_stdout = 1; } @@ -826,7 +837,7 @@ getostream(const char *ospec, const char *mname, int b if (outfmt == 'a') putc('\n', sop->ofp); } - if (xres > 0) + if (waitflush > 0) fflush(sop->ofp); } sop->reclen += noopen; /* add to length if noopen */ @@ -993,7 +1004,7 @@ done_contrib(int navg) if (using_stdout & (outfmt == 'a')) putc('\n', stdout); if (!waitflush) { - waitflush = xres; + waitflush = (yres > 0) & (xres > 1) ? 0 : xres; if (using_stdout) fflush(stdout); } @@ -1075,7 +1086,7 @@ wait_rproc(void) { struct rtproc *rtfree = NULL; fd_set readset, errset; - int nr; + ssize_t nr; struct rtproc *rt; int n; @@ -1106,23 +1117,29 @@ wait_rproc(void) continue; if (rt->buf == NULL) { rt->bsiz = treebufsiz; - rt->buf = (char *)malloc(treebufsiz); + rt->buf = (char *)malloc(rt->bsiz); } else if (rt->nbr + BUFSIZ > rt->bsiz) { if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; - else - treebufsiz = rt->bsiz += BUFSIZ; + else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0) + error(INTERNAL, + "ray buffer does not fit memory"); rt->buf = (char *)realloc(rt->buf, rt->bsiz); } if (rt->buf == NULL) error(SYSTEM, "out of memory in wait_rproc"); - nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); - if (nr <= 0) + nr = rt->bsiz - rt->nbr; + if (nr & ~0x7fffffff) /* avoid 32-bit OS issues */ + nr = 0x7fffffff; + nr = read(rt->pd.r, rt->buf+rt->nbr, nr); + if (nr < 0) + error(SYSTEM, "read error from rtrace"); + if (!nr) error(USER, "rtrace process died"); rt->nbr += nr; /* advance & check */ - if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, - "~\t~\t", 4)) { - rt->nbr -= 4; /* elide terminator */ + if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6, + "~\t~\t~\t", 6)) { + rt->nbr -= 6; /* elide terminator */ queue_raytree(rt); rtfree = rt; /* ready for next ray */ } @@ -1164,8 +1181,7 @@ trace_contribs(FILE *fin) lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - if (lastray+1 < lastray) - lastdone = lastray = 0; + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ rtp->raynum = ++lastray; /* assign ray */