--- ray/src/common/color.c 2004/09/14 02:53:50 2.15 +++ ray/src/common/color.c 2019/05/27 15:44:26 2.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: color.c,v 2.15 2004/09/14 02:53:50 greg Exp $"; +static const char RCSid[] = "$Id: color.c,v 2.21 2019/05/27 15:44:26 greg Exp $"; #endif /* * color.c - routines for color calculations. @@ -10,18 +10,17 @@ static const char RCSid[] = "$Id: color.c,v 2.15 2004/ #include "copyright.h" #include - #include - #include - #include "color.h" #ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ #undef getc #undef putc +#undef ferror #define getc getc_unlocked #define putc putc_unlocked +#define ferror ferror_unlocked #endif #define MINELEN 8 /* minimum scanline length for encoding */ @@ -30,30 +29,31 @@ static const char RCSid[] = "$Id: color.c,v 2.15 2004/ char * -tempbuffer(len) /* get a temporary buffer */ -unsigned int len; +tempbuffer( /* get a temporary buffer */ + unsigned int len +) { static char *tempbuf = NULL; static unsigned tempbuflen = 0; - if (len > tempbuflen) { - if (tempbuflen > 0) - tempbuf = (char *)realloc((void *)tempbuf, len); - else - tempbuf = (char *)malloc(len); - tempbuflen = tempbuf==NULL ? 0 : len; + if (!len | (len > tempbuflen)) { + if (tempbuflen) + free(tempbuf); + tempbuf = len ? (char *)malloc(len) : (char *)NULL; + tempbuflen = len*(tempbuf != NULL); } return(tempbuf); } int -fwritecolrs(scanline, len, fp) /* write out a colr scanline */ -register COLR *scanline; -int len; -register FILE *fp; +fwritecolrs( /* write out a colr scanline */ + COLR *scanline, + int len, + FILE *fp +) { - register int i, j, beg, cnt = 1; + int i, j, beg, cnt = 1; int c2; if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ @@ -62,18 +62,18 @@ register FILE *fp; putc(2, fp); putc(2, fp); putc(len>>8, fp); - putc(len&255, fp); + putc(len&0xff, fp); /* put components seperately */ for (i = 0; i < 4; i++) { for (j = 0; j < len; j += cnt) { /* find next run */ for (beg = j; beg < len; beg += cnt) { - for (cnt = 1; cnt < 127 && beg+cnt < len && + for (cnt = 1; (cnt < 127) & (beg+cnt < len) && scanline[beg+cnt][i] == scanline[beg][i]; cnt++) ; if (cnt >= MINRUN) break; /* long enough */ } - if (beg-j > 1 && beg-j < MINRUN) { + if ((beg-j > 1) & (beg-j < MINRUN)) { c2 = j+1; while (scanline[c2++][i] == scanline[j][i]) if (c2 == beg) { /* short run */ @@ -101,30 +101,31 @@ register FILE *fp; static int -oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ -register COLR *scanline; -int len; -register FILE *fp; +oldreadcolrs( /* read in an old-style colr scanline */ + COLR *scanline, + int len, + FILE *fp +) { - int rshift; - register int i; + int rshift = 0; + int i; - rshift = 0; - while (len > 0) { scanline[0][RED] = getc(fp); scanline[0][GRN] = getc(fp); scanline[0][BLU] = getc(fp); - scanline[0][EXP] = getc(fp); - if (feof(fp) || ferror(fp)) + scanline[0][EXP] = i = getc(fp); + if (i == EOF) return(-1); - if (scanline[0][RED] == 1 && - scanline[0][GRN] == 1 && - scanline[0][BLU] == 1) { - for (i = scanline[0][EXP] << rshift; i > 0; i--) { + if (scanline[0][GRN] == 1 && + (scanline[0][RED] == 1) & + (scanline[0][BLU] == 1)) { + i = scanline[0][EXP] << rshift; + while (i--) { copycolr(scanline[0], scanline[-1]); + if (--len <= 0) + return(0); scanline++; - len--; } rshift += 8; } else { @@ -138,12 +139,13 @@ register FILE *fp; int -freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ -register COLR *scanline; -int len; -register FILE *fp; +freadcolrs( /* read in an encoded colr scanline */ + COLR *scanline, + int len, + FILE *fp +) { - register int i, j; + int i, j; int code, val; /* determine scanline type */ if ((len < MINELEN) | (len > MAXELEN)) @@ -158,7 +160,7 @@ register FILE *fp; scanline[0][BLU] = getc(fp); if ((i = getc(fp)) == EOF) return(-1); - if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) { + if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) { scanline[0][RED] = 2; scanline[0][EXP] = i; return(oldreadcolrs(scanline+1, len-1, fp)); @@ -174,28 +176,34 @@ register FILE *fp; code &= 127; if ((val = getc(fp)) == EOF) return -1; + if (j + code > len) + return -1; /* overrun */ while (code--) scanline[j++][i] = val; - } else /* non-run */ + } else { /* non-run */ + if (j + code > len) + return -1; /* overrun */ while (code--) { if ((val = getc(fp)) == EOF) return -1; scanline[j++][i] = val; } + } } return(0); } int -fwritescan(scanline, len, fp) /* write out a scanline */ -register COLOR *scanline; -int len; -FILE *fp; +fwritescan( /* write out a scanline */ + COLOR *scanline, + int len, + FILE *fp +) { COLR *clrscan; int n; - register COLR *sp; + COLR *sp; /* get scanline buffer */ if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) return(-1); @@ -214,12 +222,13 @@ FILE *fp; int -freadscan(scanline, len, fp) /* read in a scanline */ -register COLOR *scanline; -int len; -FILE *fp; +freadscan( /* read in a scanline */ + COLOR *scanline, + int len, + FILE *fp +) { - register COLR *clrscan; + COLR *clrscan; if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) return(-1); @@ -229,10 +238,10 @@ FILE *fp; colr_color(scanline[0], clrscan[0]); while (--len > 0) { scanline++; clrscan++; - if (clrscan[0][RED] == clrscan[-1][RED] && - clrscan[0][GRN] == clrscan[-1][GRN] && - clrscan[0][BLU] == clrscan[-1][BLU] && - clrscan[0][EXP] == clrscan[-1][EXP]) + if (clrscan[0][GRN] == clrscan[-1][GRN] && + (clrscan[0][RED] == clrscan[-1][RED]) & + (clrscan[0][BLU] == clrscan[-1][BLU]) & + (clrscan[0][EXP] == clrscan[-1][EXP])) copycolor(scanline[0], scanline[-1]); else colr_color(scanline[0], clrscan[0]); @@ -242,9 +251,12 @@ FILE *fp; void -setcolr(clr, r, g, b) /* assign a short color value */ -register COLR clr; -double r, g, b; +setcolr( /* assign a short color value */ + COLR clr, + double r, + double g, + double b +) { double d; int e; @@ -258,7 +270,7 @@ double r, g, b; return; } - d = frexp(d, &e) * 255.9999 / d; + d = frexp(d, &e) * 256.0 / d; if (r > 0.0) clr[RED] = r * d; @@ -278,9 +290,10 @@ double r, g, b; void -colr_color(col, clr) /* convert short to float color */ -register COLOR col; -register COLR clr; +colr_color( /* convert short to float color */ + COLOR col, + COLR clr +) { double f; @@ -296,15 +309,17 @@ register COLR clr; int -bigdiff(c1, c2, md) /* c1 delta c2 > md? */ -register COLOR c1, c2; -double md; +bigdiff( /* c1 delta c2 > md? */ + COLOR c1, + COLOR c2, + double md +) { - register int i; + int i; for (i = 0; i < 3; i++) if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || - colval(c2,i)-colval(c1,i) > md*colval(c1,i)) + colval(c2,i)-colval(c1,i) > md*colval(c1,i)) return(1); return(0); }