--- ray/src/rt/ambient.c 1995/11/21 14:28:22 2.35 +++ ray/src/rt/ambient.c 1996/05/28 14:24:44 2.37 @@ -21,9 +21,6 @@ static char SCCSid[] = "$SunId$ LBL"; #ifndef OCTSCALE #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ #endif -#ifndef AMBVWT -#define AMBVWT 250 /* relative ambient value weight (# calcs) */ -#endif typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -124,7 +121,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -140,11 +137,19 @@ char *afile; /* open ambient file */ if ((ambfp = fopen(afile, "r+")) != NULL) { initambfile(0); - headlen = ftell(ambfp); + pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); + pos += (long)nambvals*AMBVALSIZ; + flen = lseek(fileno(ambfp), 0L, 2); + if (flen != pos) { + error(WARNING, + "ignoring last %ld values in ambient file (corrupted)", + (flen - pos)/AMBVALSIZ); + fseek(ambfp, pos, 0); + ftruncate(fileno(ambfp), pos); + } nambshare = nambvals; } else if ((ambfp = fopen(afile, "w+")) != NULL) initambfile(1); @@ -222,14 +227,12 @@ FVECT nrm; return; dumbamb: /* return global value */ copycolor(acol, ambval); -#if AMBVWT - if (nambvals == 0) + if (ambvwt <= 0 | nambvals == 0) return; - scalecolor(acol, (double)AMBVWT); + scalecolor(acol, (double)ambvwt); addcolor(acol, avsum); /* average in computations */ - d = 1.0/(AMBVWT+nambvals); + d = 1.0/(ambvwt+nambvals); scalecolor(acol, d); -#endif } @@ -736,7 +739,13 @@ ambsync() /* synchronize ambient file */ if (fseek(ambinp, lastpos, 0) < 0) goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ - readambval(&avs, ambinp); + if (!readambval(&avs, ambinp)) { + sprintf(errmsg, + "ambient file corrupted near character %ld", + flen - n); + error(WARNING, errmsg); + break; + } avinsert(avstore(&avs)); n -= AMBVALSIZ; }