--- ray/src/common/color.c 1989/02/02 10:34:29 1.1 +++ ray/src/common/color.c 1989/10/20 16:45:26 1.6 @@ -107,6 +107,52 @@ register COLOR rgbcolor, ciecolor; #endif +fputresolu(ord, xres, yres, fp) /* put x and y resolution */ +register int ord; +int xres, yres; +FILE *fp; +{ + if (ord&YMAJOR) + fprintf(fp, "%cY %d %cX %d\n", + ord&YDECR ? '-' : '+', yres, + ord&XDECR ? '-' : '+', xres); + else + fprintf(fp, "%cX %d %cY %d\n", + ord&XDECR ? '-' : '+', xres, + ord&YDECR ? '-' : '+', yres); +} + + +fgetresolu(xrp, yrp, fp) /* get x and y resolution */ +int *xrp, *yrp; +FILE *fp; +{ + char buf[64], *xndx, *yndx; + register char *cp; + register int ord; + + if (fgets(buf, sizeof(buf), fp) == NULL) + return(-1); + xndx = yndx = NULL; + for (cp = buf+1; *cp; cp++) + if (*cp == 'X') + xndx = cp; + else if (*cp == 'Y') + yndx = cp; + if (xndx == NULL || yndx == NULL) + return(-1); + ord = 0; + if (xndx > yndx) ord |= YMAJOR; + if (xndx[-1] == '-') ord |= XDECR; + if (yndx[-1] == '-') ord |= YDECR; + if ((*xrp = atoi(xndx+1)) <= 0) + return(-1); + if ((*yrp = atoi(yndx+1)) <= 0) + return(-1); + return(ord); +} + + fwritecolrs(scanline, len, fp) /* write out a colr scanline */ register COLR *scanline; int len; @@ -289,7 +335,7 @@ double r, g, b; d = r > g ? r : g; if (b > d) d = b; - if (d <= 0.0) { + if (d <= 1e-32) { clr[RED] = clr[GRN] = clr[BLU] = 0; clr[EXP] = 0; return; @@ -308,12 +354,12 @@ colr_color(col, clr) /* convert short to float color register COLOR col; register COLR clr; { - double ldexp(), f; + double f; if (clr[EXP] == 0) col[RED] = col[GRN] = col[BLU] = 0.0; else { - f = ldexp(1.0, clr[EXP]-(COLXS+8)); + f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); col[RED] = (clr[RED] + 0.5)*f; col[GRN] = (clr[GRN] + 0.5)*f; col[BLU] = (clr[BLU] + 0.5)*f; @@ -321,31 +367,37 @@ register COLR clr; } -#ifdef FREXP -double -frexp(x, ip) /* call it paranoia, I've seen the lib version */ -register double x; -int *ip; +int +colr_norm(clr, nclr) /* normalize a short color, return shift */ +COLR clr, nclr; { - int neg; - register int i; + register int c; + register int shift = clr[EXP]-COLXS; - if (neg = (x < 0.0)) - x = -x; - else if (x == 0.0) { - *ip = 0; - return(0.0); + if (shift > 0) { + if (shift >= 8) { + nclr[RED] = nclr[GRN] = nclr[BLU] = 255; + } else { + c = clr[RED] << shift; + nclr[RED] = c > 255 ? 255 : c; + c = clr[GRN] << shift; + nclr[GRN] = c > 255 ? 255 : c; + c = clr[BLU] << shift; + nclr[BLU] = c > 255 ? 255 : c; + } + } else if (shift < 0) { + if (shift <= -8) { + nclr[RED] = nclr[GRN] = nclr[BLU] = 0; + } else { + nclr[RED] = clr[RED] >> -shift; + nclr[GRN] = clr[GRN] >> -shift; + nclr[BLU] = clr[BLU] >> -shift; + } + } else { + nclr[RED] = clr[RED]; + nclr[GRN] = clr[GRN]; + nclr[BLU] = clr[BLU]; } - if (x < 0.5) - for (i = 0; x < 0.5; i--) - x *= 2.0; - else - for (i = 0; x >= 1.0; i++) - x /= 2.0; - *ip = i; - if (neg) - return(-x); - else - return(x); + nclr[EXP] = COLXS; + return(shift); } -#endif