--- ray/src/rt/rc2.c 2012/06/09 07:16:47 2.1 +++ ray/src/rt/rc2.c 2016/03/06 01:13:18 2.17 @@ -1,15 +1,36 @@ #ifndef lint -static const char RCSid[] = "$Id: rc2.c,v 2.1 2012/06/09 07:16:47 greg Exp $"; +static const char RCSid[] = "$Id: rc2.c,v 2.17 2016/03/06 01:13:18 schorsch Exp $"; #endif /* * Accumulate ray contributions for a set of materials * File i/o and recovery */ +#include +#include "platform.h" #include "rcontrib.h" #include "resolu.h" -#include "platform.h" +/* Close output stream and free record */ +static void +closestream(void *p) +{ + STREAMOUT *sop = (STREAMOUT *)p; + + if (sop->ofp != NULL) { + int status = 0; + if (sop->outpipe) + status = pclose(sop->ofp); + else if (sop->ofp != stdout) + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); + } + free(p); +} + +LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ + #define OF_MODIFIER 01 #define OF_BIN 02 @@ -83,15 +104,16 @@ printheader(FILE *fout, const char *info) if (octname[strlen(octname)-1] != '\n') fputc('\n', fout); } else { - FILE *fin = fopen(octname, "r"); + FILE *fin = fopen(octname, (outfmt=='a') ? "r" : "rb"); if (fin == NULL) quit(1); - checkheader(fin, "ignore", fout); + checkheader(fin, OCTFMT, fout); fclose(fin); } printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); + fputs("NCOMP=3\n", fout); /* always RGB */ if (info != NULL) /* add extra info if given */ fputs(info, fout); fputformat(formstr(outfmt), fout); @@ -112,20 +134,31 @@ printresolu(FILE *fout, int xr, int yr) STREAMOUT * getostream(const char *ospec, const char *mname, int bn, int noopen) { - /* static const DCOLOR nocontrib = BLKCOLOR; */ static STREAMOUT stdos; + char info[1024]; int ofl; char oname[1024]; LUENT *lep; STREAMOUT *sop; + char *cp; if (ospec == NULL) { /* use stdout? */ - if (!noopen && !using_stdout) { + if (!noopen & !using_stdout) { if (outfmt != 'a') SET_FILE_BINARY(stdout); - if (header) - printheader(stdout, NULL); - printresolu(stdout, xres, yres); + if (header) { + cp = info; + if (yres > 0) { + sprintf(cp, "NROWS=%d\n", yres * + (xres + !xres) ); + while (*cp) ++cp; + } + if ((xres <= 0) | (stdos.reclen > 1)) + sprintf(cp, "NCOLS=%d\n", stdos.reclen); + printheader(stdout, info); + } + if (stdos.reclen == 1) + printresolu(stdout, xres, yres); if (waitflush > 0) fflush(stdout); stdos.xr = xres; stdos.yr = yres; @@ -163,7 +196,6 @@ getostream(const char *ospec, const char *mname, int b } } if (!noopen && sop->ofp == NULL) { /* open output stream */ - long i; if (oname[0] == '!') /* output to command */ sop->ofp = popen(oname+1, "w"); else /* else open file */ @@ -175,9 +207,11 @@ getostream(const char *ospec, const char *mname, int b #ifdef getc_unlocked flockfile(sop->ofp); /* avoid lock/unlock overhead */ #endif + if (accumulate > 0) { /* global resolution */ + sop->xr = xres; sop->yr = yres; + } if (header) { - char info[512]; - char *cp = info; + cp = info; if (ofl & OF_MODIFIER || sop->reclen == 1) { sprintf(cp, "MODIFIER=%s\n", mname); while (*cp) ++cp; @@ -186,24 +220,17 @@ getostream(const char *ospec, const char *mname, int b sprintf(cp, "BIN=%d\n", bn); while (*cp) ++cp; } - *cp = '\0'; + if (sop->yr > 0) { + sprintf(cp, "NROWS=%d\n", sop->yr * + (sop->xr + !sop->xr) ); + while (*cp) ++cp; + } + if ((sop->xr <= 0) | (sop->reclen > 1)) + sprintf(cp, "NCOLS=%d\n", sop->reclen); printheader(sop->ofp, info); } - if (accumulate > 0) { /* global resolution */ - sop->xr = xres; sop->yr = yres; - } - printresolu(sop->ofp, sop->xr, sop->yr); -#if 0 - /* play catch-up */ - for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) { - int j = sop->reclen; - if (j <= 0) j = 1; - while (j--) - put_contrib(nocontrib, sop->ofp); - if (outfmt == 'a') - putc('\n', sop->ofp); - } -#endif + if (sop->reclen == 1) + printresolu(sop->ofp, sop->xr, sop->yr); if (waitflush > 0) fflush(sop->ofp); } @@ -256,7 +283,7 @@ static void put_contrib(const DCOLOR cnt, FILE *fout) { double sf = 1; - float fv[3]; + COLOR fv; COLR cv; if (accumulate > 1) @@ -272,15 +299,17 @@ put_contrib(const DCOLOR cnt, FILE *fout) break; case 'f': if (accumulate > 1) { - fv[0] = sf*cnt[0]; fv[1] = sf*cnt[1]; fv[2] = sf*cnt[2]; + copycolor(fv, cnt); + scalecolor(fv, sf); } else copycolor(fv, cnt); fwrite(fv, sizeof(float), 3, fout); break; case 'd': if (accumulate > 1) { - double dv[3]; - dv[0] = sf*cnt[0]; dv[1] = sf*cnt[1]; dv[2] = sf*cnt[2]; + DCOLOR dv; + copycolor(dv, cnt); + scalecolor(dv, sf); fwrite(dv, sizeof(double), 3, fout); } else fwrite(cnt, sizeof(double), 3, fout); @@ -302,19 +331,20 @@ put_contrib(const DCOLOR cnt, FILE *fout) void mod_output(MODCONT *mp) { - STREAMOUT *sop = getostream(mp->outspec, mp->modname, 0,0); + STREAMOUT *sop = getostream(mp->outspec, mp->modname, 0, 0); int j; put_contrib(mp->cbin[0], sop->ofp); if (mp->nbins > 3 && /* minor optimization */ - sop == getostream(mp->outspec, mp->modname, 1,0)) + sop == getostream(mp->outspec, mp->modname, 1, 0)) { for (j = 1; j < mp->nbins; j++) put_contrib(mp->cbin[j], sop->ofp); - else + } else { for (j = 1; j < mp->nbins; j++) { - sop = getostream(mp->outspec, mp->modname,j,0); + sop = getostream(mp->outspec, mp->modname, j, 0); put_contrib(mp->cbin[j], sop->ofp); } + } } @@ -341,7 +371,7 @@ void end_record() { --waitflush; - lu_doall(&ofiletab, puteol, NULL); + lu_doall(&ofiletab, &puteol, NULL); if (using_stdout & (outfmt == 'a')) putc('\n', stdout); if (!waitflush) { @@ -453,7 +483,7 @@ reload_output() oname); error(USER, errmsg); } - if ((sout.xr > 0) & (sout.yr > 0) && + if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { @@ -491,7 +521,7 @@ reload_output() *(STREAMOUT *)oent->data = sout; } } - lu_doall(&ofiletab, myclose, NULL); /* close all files */ + lu_doall(&ofiletab, &myclose, NULL); /* close all files */ } @@ -563,6 +593,8 @@ recover_output() } else { sout.reclen = 0; sout.outpipe = 0; + sout.xr = xres; + sout.yr = yres; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ @@ -603,8 +635,7 @@ recover_output() oname); error(USER, errmsg); } - sout.xr = xres; sout.yr = yres; - if ((sout.xr > 0) & (sout.yr > 0) && + if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { @@ -647,14 +678,15 @@ recover_output() } /* seek on all files */ nvals = lastout * outvsiz; - lu_doall(&ofiletab, myseeko, &nvals); + lu_doall(&ofiletab, &myseeko, &nvals); /* skip repeated input */ + lastout *= accumulate; for (nvals = 0; nvals < lastout; nvals++) { FVECT vdummy; if (getvec(vdummy) < 0 || getvec(vdummy) < 0) error(USER, "unexpected EOF on input"); } - lastray = lastdone = lastout * accumulate; + lastray = lastdone = (RNUMBER)lastout; if (raysleft) raysleft -= lastray; }