1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
23 |
|
#define part(cn) ((cn)>>2) |
24 |
|
#define prim(cn) ((cn)&3) |
25 |
|
/* our color table (global) */ |
26 |
< |
BYTE clrtab[256][3]; |
26 |
> |
extern BYTE clrtab[256][3]; |
27 |
|
/* histogram of colors / color assignments */ |
28 |
|
static unsigned histo[NRED][NGRN][NBLU]; |
29 |
|
#define cndx(c) histo[((c)[RED]*NRED)>>8][((c)[GRN]*NGRN)>>8][((c)[BLU]*NBLU)>>8] |
32 |
|
/* maximum propagated error during dithering */ |
33 |
|
#define MAXERR 20 |
34 |
|
/* define CLOSEST to get closest colors */ |
35 |
< |
#define CLOSEST 1 |
35 |
> |
#ifndef CLOSEST |
36 |
> |
#ifdef SPEED |
37 |
> |
#if SPEED > 8 |
38 |
> |
#define CLOSEST 1 /* this step takes a little longer */ |
39 |
> |
#endif |
40 |
> |
#endif |
41 |
> |
#endif |
42 |
|
|
43 |
+ |
static cut(), mktabent(), closest(), addneigh(), setclosest(); |
44 |
+ |
static int split(); |
45 |
+ |
static unsigned dist(); |
46 |
|
|
47 |
< |
new_histo() /* clear our histogram */ |
47 |
> |
|
48 |
> |
new_histo(n) /* clear our histogram */ |
49 |
> |
int n; |
50 |
|
{ |
51 |
|
bzero((char *)histo, sizeof(histo)); |
52 |
+ |
return(0); |
53 |
|
} |
54 |
|
|
55 |
|
|
83 |
|
#ifdef CLOSEST |
84 |
|
closest(ncolors); /* ensure colors picked are closest */ |
85 |
|
#endif |
86 |
+ |
/* reset dithering function */ |
87 |
+ |
dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
88 |
|
/* return new color table size */ |
89 |
|
return(ncolors); |
90 |
|
} |
115 |
|
register COLR *cs; |
116 |
|
int n; |
117 |
|
{ |
118 |
< |
static short (*cerr)[3]; |
118 |
> |
static short (*cerr)[3] = NULL; |
119 |
|
static int N = 0; |
120 |
|
int err[3], errp[3]; |
121 |
|
register int x, i; |
122 |
|
|
123 |
|
if (n != N) { /* get error propogation array */ |
124 |
< |
if (N) |
125 |
< |
cerr = (short (*)[3])realloc((char *)cerr, |
126 |
< |
3*n*sizeof(short)); |
127 |
< |
else |
124 |
> |
if (N) { |
125 |
> |
free((char *)cerr); |
126 |
> |
cerr = NULL; |
127 |
> |
} |
128 |
> |
if (n) |
129 |
|
cerr = (short (*)[3])malloc(3*n*sizeof(short)); |
130 |
|
if (cerr == NULL) { |
131 |
|
N = 0; |
190 |
|
#define c0 r |
191 |
|
register int r, g, b; |
192 |
|
int pri; |
193 |
< |
int t[HMAX], med; |
193 |
> |
long t[HMAX], med; |
194 |
|
/* find dominant axis */ |
195 |
|
pri = RED; |
196 |
|
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |
246 |
|
int p; |
247 |
|
register int box[3][2]; |
248 |
|
{ |
249 |
< |
long sum[3]; |
250 |
< |
int r, g, n; |
251 |
< |
register int b, c; |
249 |
> |
unsigned long sum[3]; |
250 |
> |
unsigned r, g; |
251 |
> |
unsigned long n; |
252 |
> |
register unsigned b, c; |
253 |
|
/* sum pixels in box */ |
254 |
|
n = 0; |
255 |
|
sum[RED] = sum[GRN] = sum[BLU] = 0; |
264 |
|
} |
265 |
|
histo[r][g][b] = p; /* assign pixel */ |
266 |
|
} |
267 |
+ |
if (n >= (1L<<23)/HMAX) { /* avoid overflow */ |
268 |
+ |
sum[RED] /= n; |
269 |
+ |
sum[GRN] /= n; |
270 |
+ |
sum[BLU] /= n; |
271 |
+ |
n = 1; |
272 |
+ |
} |
273 |
|
if (n) { /* compute average */ |
274 |
|
clrtab[p][RED] = sum[RED]*256/NRED/n; |
275 |
|
clrtab[p][GRN] = sum[GRN]*256/NGRN/n; |
301 |
|
neigh[i][0] = i; /* identity is terminator */ |
302 |
|
} |
303 |
|
/* make neighbor lists */ |
304 |
< |
for (r = 0; r < NRED-1; r++) |
305 |
< |
for (g = 0; g < NGRN-1; g++) |
306 |
< |
for (b = 0; b < NBLU-1; b++) { |
307 |
< |
if (histo[r][g][b] != histo[r+1][g][b]) |
304 |
> |
for (r = 0; r < NRED; r++) |
305 |
> |
for (g = 0; g < NGRN; g++) |
306 |
> |
for (b = 0; b < NBLU; b++) { |
307 |
> |
if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b]) |
308 |
|
addneigh(neigh, histo[r][g][b], histo[r+1][g][b]); |
309 |
< |
if (histo[r][g][b] != histo[r][g+1][b]) |
309 |
> |
if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b]) |
310 |
|
addneigh(neigh, histo[r][g][b], histo[r][g+1][b]); |
311 |
< |
if (histo[r][g][b] != histo[r][g][b+1]) |
311 |
> |
if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1]) |
312 |
|
addneigh(neigh, histo[r][g][b], histo[r][g][b+1]); |
313 |
|
} |
314 |
|
/* assign closest values */ |
357 |
|
register BYTE col[3]; |
358 |
|
int r, g, b; |
359 |
|
{ |
360 |
< |
register unsigned tmp; |
360 |
> |
register int tmp; |
361 |
|
register unsigned sum; |
362 |
|
|
363 |
|
tmp = col[RED]*NRED/256 - r; |