--- ray/src/px/clrtab.c 1992/10/13 09:07:51 2.2 +++ ray/src/px/clrtab.c 1993/06/10 15:45:18 2.7 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1993 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -32,7 +32,13 @@ static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; /* maximum propagated error during dithering */ #define MAXERR 20 /* define CLOSEST to get closest colors */ -#define CLOSEST 1 +#ifndef CLOSEST +#ifdef SPEED +#if SPEED > 8 +#define CLOSEST 1 /* this step takes a little longer */ +#endif +#endif +#endif new_histo() /* clear our histogram */ @@ -175,7 +181,7 @@ register int box[3][2]; #define c0 r register int r, g, b; int pri; - int t[HMAX], med; + long t[HMAX], med; /* find dominant axis */ pri = RED; if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) @@ -231,9 +237,10 @@ mktabent(p, box) /* compute average color for box and int p; register int box[3][2]; { - long sum[3]; - int r, g, n; - register int b, c; + unsigned long sum[3]; + unsigned r, g; + unsigned long n; + register unsigned b, c; /* sum pixels in box */ n = 0; sum[RED] = sum[GRN] = sum[BLU] = 0; @@ -248,6 +255,12 @@ register int box[3][2]; } histo[r][g][b] = p; /* assign pixel */ } + if (n >= (1L<<23)/HMAX) { /* avoid overflow */ + sum[RED] /= n; + sum[GRN] /= n; + sum[BLU] /= n; + n = 1; + } if (n) { /* compute average */ clrtab[p][RED] = sum[RED]*256/NRED/n; clrtab[p][GRN] = sum[GRN]*256/NGRN/n; @@ -279,14 +292,14 @@ int n; neigh[i][0] = i; /* identity is terminator */ } /* make neighbor lists */ - for (r = 0; r < NRED-1; r++) - for (g = 0; g < NGRN-1; g++) - for (b = 0; b < NBLU-1; b++) { - if (histo[r][g][b] != histo[r+1][g][b]) + for (r = 0; r < NRED; r++) + for (g = 0; g < NGRN; g++) + for (b = 0; b < NBLU; b++) { + if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b]) addneigh(neigh, histo[r][g][b], histo[r+1][g][b]); - if (histo[r][g][b] != histo[r][g+1][b]) + if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b]) addneigh(neigh, histo[r][g][b], histo[r][g+1][b]); - if (histo[r][g][b] != histo[r][g][b+1]) + if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1]) addneigh(neigh, histo[r][g][b], histo[r][g][b+1]); } /* assign closest values */ @@ -335,7 +348,7 @@ dist(col, r, g, b) /* find distance from clrtab entry register BYTE col[3]; int r, g, b; { - register unsigned tmp; + register int tmp; register unsigned sum; tmp = col[RED]*NRED/256 - r;