--- ray/src/px/clrtab.c 1992/10/13 11:36:20 2.3 +++ ray/src/px/clrtab.c 2003/06/30 14:59:12 2.15 @@ -1,16 +1,17 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: clrtab.c,v 2.15 2003/06/30 14:59:12 schorsch Exp $"; #endif - /* * Simple median-cut color quantization based on colortab.c */ -#include "standard.h" +#include "copyright.h" +#include + +#include "standard.h" #include "color.h" + /* histogram resolution */ #define NRED 36 #define NGRN 48 @@ -23,7 +24,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define part(cn) ((cn)>>2) #define prim(cn) ((cn)&3) /* our color table (global) */ -BYTE clrtab[256][3]; +extern BYTE clrtab[256][3]; /* histogram of colors / color assignments */ static unsigned histo[NRED][NGRN][NBLU]; #define cndx(c) histo[((c)[RED]*NRED)>>8][((c)[GRN]*NGRN)>>8][((c)[BLU]*NBLU)>>8] @@ -32,12 +33,24 @@ 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 +static cut(), mktabent(), closest(), addneigh(), setclosest(); +static int split(); +static unsigned dist(); -new_histo() /* clear our histogram */ + +new_histo(n) /* clear our histogram */ +int n; { - bzero((char *)histo, sizeof(histo)); + memset((void *)histo, '\0', sizeof(histo)); + return(0); } @@ -71,6 +84,8 @@ int ncolors; #ifdef CLOSEST closest(ncolors); /* ensure colors picked are closest */ #endif + /* reset dithering function */ + dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); /* return new color table size */ return(ncolors); } @@ -101,16 +116,17 @@ register BYTE *bs; register COLR *cs; int n; { - static short (*cerr)[3]; + static short (*cerr)[3] = NULL; static int N = 0; int err[3], errp[3]; register int x, i; if (n != N) { /* get error propogation array */ - if (N) - cerr = (short (*)[3])realloc((char *)cerr, - 3*n*sizeof(short)); - else + if (N) { + free((void *)cerr); + cerr = NULL; + } + if (n) cerr = (short (*)[3])malloc(3*n*sizeof(short)); if (cerr == NULL) { N = 0; @@ -118,7 +134,7 @@ int n; return; } N = n; - bzero((char *)cerr, 3*N*sizeof(short)); + memset((void *)cerr, '\0', 3*N*sizeof(short)); } err[0] = err[1] = err[2] = 0; for (x = 0; x < n; x++) { @@ -157,7 +173,7 @@ int c0, c1; } /* split box */ branch = split(box); - bcopy((char *)box, (char *)kb, sizeof(kb)); + memcpy((void *)kb, (void *)box, sizeof(kb)); /* do left (lesser) branch */ kb[prim(branch)][1] = part(branch); cut(kb, c0, (c0+c1)>>1); @@ -175,7 +191,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 +247,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 +265,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; @@ -318,7 +341,8 @@ int j; if (nl[i][t] == i) { /* add to list */ nl[i][t++] = j; if (t % NBSIZ == 0) { /* enlarge list */ - if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL) + if ((nnl = realloc((void *)nl[i], + t+NBSIZ)) == NULL) t--; else nl[i] = (BYTE *)nnl; @@ -335,7 +359,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;