| 1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Simple median-cut color quantization based on colortab.c |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
< |
#include "standard.h" |
| 8 |
> |
#include "copyright.h" |
| 9 |
|
|
| 10 |
+ |
#include <string.h> |
| 11 |
+ |
|
| 12 |
+ |
#include "standard.h" |
| 13 |
|
#include "color.h" |
| 14 |
+ |
|
| 15 |
|
/* histogram resolution */ |
| 16 |
|
#define NRED 36 |
| 17 |
|
#define NGRN 48 |
| 24 |
|
#define part(cn) ((cn)>>2) |
| 25 |
|
#define prim(cn) ((cn)&3) |
| 26 |
|
/* our color table (global) */ |
| 27 |
< |
BYTE clrtab[256][3]; |
| 27 |
> |
extern BYTE clrtab[256][3]; |
| 28 |
|
/* histogram of colors / color assignments */ |
| 29 |
|
static unsigned histo[NRED][NGRN][NBLU]; |
| 30 |
|
#define cndx(c) histo[((c)[RED]*NRED)>>8][((c)[GRN]*NGRN)>>8][((c)[BLU]*NBLU)>>8] |
| 33 |
|
/* maximum propagated error during dithering */ |
| 34 |
|
#define MAXERR 20 |
| 35 |
|
/* define CLOSEST to get closest colors */ |
| 36 |
< |
#define CLOSEST 1 |
| 36 |
> |
#ifndef CLOSEST |
| 37 |
> |
#ifdef SPEED |
| 38 |
> |
#if SPEED > 8 |
| 39 |
> |
#define CLOSEST 1 /* this step takes a little longer */ |
| 40 |
> |
#endif |
| 41 |
> |
#endif |
| 42 |
> |
#endif |
| 43 |
|
|
| 44 |
+ |
static cut(), mktabent(), closest(), addneigh(), setclosest(); |
| 45 |
+ |
static int split(); |
| 46 |
+ |
static unsigned dist(); |
| 47 |
|
|
| 48 |
< |
new_histo() /* clear our histogram */ |
| 48 |
> |
|
| 49 |
> |
new_histo(n) /* clear our histogram */ |
| 50 |
> |
int n; |
| 51 |
|
{ |
| 52 |
< |
bzero((char *)histo, sizeof(histo)); |
| 52 |
> |
memset((void *)histo, '\0', sizeof(histo)); |
| 53 |
> |
return(0); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
|
| 84 |
|
#ifdef CLOSEST |
| 85 |
|
closest(ncolors); /* ensure colors picked are closest */ |
| 86 |
|
#endif |
| 87 |
+ |
/* reset dithering function */ |
| 88 |
+ |
dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
| 89 |
|
/* return new color table size */ |
| 90 |
|
return(ncolors); |
| 91 |
|
} |
| 116 |
|
register COLR *cs; |
| 117 |
|
int n; |
| 118 |
|
{ |
| 119 |
< |
static short (*cerr)[3]; |
| 119 |
> |
static short (*cerr)[3] = NULL; |
| 120 |
|
static int N = 0; |
| 121 |
|
int err[3], errp[3]; |
| 122 |
|
register int x, i; |
| 123 |
|
|
| 124 |
|
if (n != N) { /* get error propogation array */ |
| 125 |
< |
if (N) |
| 126 |
< |
cerr = (short (*)[3])realloc((char *)cerr, |
| 127 |
< |
3*n*sizeof(short)); |
| 128 |
< |
else |
| 125 |
> |
if (N) { |
| 126 |
> |
free((void *)cerr); |
| 127 |
> |
cerr = NULL; |
| 128 |
> |
} |
| 129 |
> |
if (n) |
| 130 |
|
cerr = (short (*)[3])malloc(3*n*sizeof(short)); |
| 131 |
|
if (cerr == NULL) { |
| 132 |
|
N = 0; |
| 134 |
|
return; |
| 135 |
|
} |
| 136 |
|
N = n; |
| 137 |
< |
bzero((char *)cerr, 3*N*sizeof(short)); |
| 137 |
> |
memset((void *)cerr, '\0', 3*N*sizeof(short)); |
| 138 |
|
} |
| 139 |
|
err[0] = err[1] = err[2] = 0; |
| 140 |
|
for (x = 0; x < n; x++) { |
| 173 |
|
} |
| 174 |
|
/* split box */ |
| 175 |
|
branch = split(box); |
| 176 |
< |
bcopy((char *)box, (char *)kb, sizeof(kb)); |
| 176 |
> |
memcpy((void *)kb, (void *)box, sizeof(kb)); |
| 177 |
|
/* do left (lesser) branch */ |
| 178 |
|
kb[prim(branch)][1] = part(branch); |
| 179 |
|
cut(kb, c0, (c0+c1)>>1); |
| 191 |
|
#define c0 r |
| 192 |
|
register int r, g, b; |
| 193 |
|
int pri; |
| 194 |
< |
int t[HMAX], med; |
| 194 |
> |
long t[HMAX], med; |
| 195 |
|
/* find dominant axis */ |
| 196 |
|
pri = RED; |
| 197 |
|
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |
| 247 |
|
int p; |
| 248 |
|
register int box[3][2]; |
| 249 |
|
{ |
| 250 |
< |
long sum[3]; |
| 251 |
< |
int r, g, n; |
| 252 |
< |
register int b, c; |
| 250 |
> |
unsigned long sum[3]; |
| 251 |
> |
unsigned r, g; |
| 252 |
> |
unsigned long n; |
| 253 |
> |
register unsigned b, c; |
| 254 |
|
/* sum pixels in box */ |
| 255 |
|
n = 0; |
| 256 |
|
sum[RED] = sum[GRN] = sum[BLU] = 0; |
| 265 |
|
} |
| 266 |
|
histo[r][g][b] = p; /* assign pixel */ |
| 267 |
|
} |
| 268 |
+ |
if (n >= (1L<<23)/HMAX) { /* avoid overflow */ |
| 269 |
+ |
sum[RED] /= n; |
| 270 |
+ |
sum[GRN] /= n; |
| 271 |
+ |
sum[BLU] /= n; |
| 272 |
+ |
n = 1; |
| 273 |
+ |
} |
| 274 |
|
if (n) { /* compute average */ |
| 275 |
|
clrtab[p][RED] = sum[RED]*256/NRED/n; |
| 276 |
|
clrtab[p][GRN] = sum[GRN]*256/NGRN/n; |
| 302 |
|
neigh[i][0] = i; /* identity is terminator */ |
| 303 |
|
} |
| 304 |
|
/* make neighbor lists */ |
| 305 |
< |
for (r = 0; r < NRED-1; r++) |
| 306 |
< |
for (g = 0; g < NGRN-1; g++) |
| 307 |
< |
for (b = 0; b < NBLU-1; b++) { |
| 308 |
< |
if (histo[r][g][b] != histo[r+1][g][b]) |
| 305 |
> |
for (r = 0; r < NRED; r++) |
| 306 |
> |
for (g = 0; g < NGRN; g++) |
| 307 |
> |
for (b = 0; b < NBLU; b++) { |
| 308 |
> |
if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b]) |
| 309 |
|
addneigh(neigh, histo[r][g][b], histo[r+1][g][b]); |
| 310 |
< |
if (histo[r][g][b] != histo[r][g+1][b]) |
| 310 |
> |
if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b]) |
| 311 |
|
addneigh(neigh, histo[r][g][b], histo[r][g+1][b]); |
| 312 |
< |
if (histo[r][g][b] != histo[r][g][b+1]) |
| 312 |
> |
if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1]) |
| 313 |
|
addneigh(neigh, histo[r][g][b], histo[r][g][b+1]); |
| 314 |
|
} |
| 315 |
|
/* assign closest values */ |
| 341 |
|
if (nl[i][t] == i) { /* add to list */ |
| 342 |
|
nl[i][t++] = j; |
| 343 |
|
if (t % NBSIZ == 0) { /* enlarge list */ |
| 344 |
< |
if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL) |
| 344 |
> |
if ((nnl = realloc((void *)nl[i], |
| 345 |
> |
t+NBSIZ)) == NULL) |
| 346 |
|
t--; |
| 347 |
|
else |
| 348 |
|
nl[i] = (BYTE *)nnl; |
| 359 |
|
register BYTE col[3]; |
| 360 |
|
int r, g, b; |
| 361 |
|
{ |
| 362 |
< |
register unsigned tmp; |
| 362 |
> |
register int tmp; |
| 363 |
|
register unsigned sum; |
| 364 |
|
|
| 365 |
|
tmp = col[RED]*NRED/256 - r; |