| 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"; |
| 31 |
|
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
| 32 |
|
/* maximum propagated error during dithering */ |
| 33 |
|
#define MAXERR 20 |
| 34 |
+ |
/* define CLOSEST to get closest colors */ |
| 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 |
|
|
| 44 |
|
new_histo() /* clear our histogram */ |
| 74 |
|
ncolors = 256; |
| 75 |
|
/* partition color space */ |
| 76 |
|
cut(CLRCUBE, 0, ncolors); |
| 77 |
+ |
#ifdef CLOSEST |
| 78 |
+ |
closest(ncolors); /* ensure colors picked are closest */ |
| 79 |
+ |
#endif |
| 80 |
|
/* return new color table size */ |
| 81 |
|
return(ncolors); |
| 82 |
|
} |
| 181 |
|
#define c0 r |
| 182 |
|
register int r, g, b; |
| 183 |
|
int pri; |
| 184 |
< |
int t[HMAX], med; |
| 184 |
> |
long t[HMAX], med; |
| 185 |
|
/* find dominant axis */ |
| 186 |
|
pri = RED; |
| 187 |
|
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |
| 237 |
|
int p; |
| 238 |
|
register int box[3][2]; |
| 239 |
|
{ |
| 240 |
< |
long sum[3]; |
| 241 |
< |
int r, g, n; |
| 242 |
< |
register int b, c; |
| 240 |
> |
unsigned long sum[3]; |
| 241 |
> |
unsigned r, g; |
| 242 |
> |
unsigned long n; |
| 243 |
> |
register unsigned b, c; |
| 244 |
|
/* sum pixels in box */ |
| 245 |
|
n = 0; |
| 246 |
|
sum[RED] = sum[GRN] = sum[BLU] = 0; |
| 255 |
|
} |
| 256 |
|
histo[r][g][b] = p; /* assign pixel */ |
| 257 |
|
} |
| 258 |
+ |
if (n >= (1L<<23)/HMAX) { /* avoid overflow */ |
| 259 |
+ |
sum[RED] /= n; |
| 260 |
+ |
sum[GRN] /= n; |
| 261 |
+ |
sum[BLU] /= n; |
| 262 |
+ |
n = 1; |
| 263 |
+ |
} |
| 264 |
|
if (n) { /* compute average */ |
| 265 |
|
clrtab[p][RED] = sum[RED]*256/NRED/n; |
| 266 |
|
clrtab[p][GRN] = sum[GRN]*256/NGRN/n; |
| 271 |
|
clrtab[p][BLU] = (box[BLU][0]+box[BLU][1])*256/NBLU/2; |
| 272 |
|
} |
| 273 |
|
} |
| 274 |
+ |
|
| 275 |
+ |
|
| 276 |
+ |
#ifdef CLOSEST |
| 277 |
+ |
#define NBSIZ 32 |
| 278 |
+ |
static |
| 279 |
+ |
closest(n) /* make sure we have the closest colors */ |
| 280 |
+ |
int n; |
| 281 |
+ |
{ |
| 282 |
+ |
BYTE *neigh[256]; |
| 283 |
+ |
register int r, g, b; |
| 284 |
+ |
#define i r |
| 285 |
+ |
/* get space for neighbor lists */ |
| 286 |
+ |
for (i = 0; i < n; i++) { |
| 287 |
+ |
if ((neigh[i] = (BYTE *)malloc(NBSIZ)) == NULL) { |
| 288 |
+ |
while (i--) |
| 289 |
+ |
free(neigh[i]); |
| 290 |
+ |
return; /* ENOMEM -- abandon effort */ |
| 291 |
+ |
} |
| 292 |
+ |
neigh[i][0] = i; /* identity is terminator */ |
| 293 |
+ |
} |
| 294 |
+ |
/* make neighbor lists */ |
| 295 |
+ |
for (r = 0; r < NRED; r++) |
| 296 |
+ |
for (g = 0; g < NGRN; g++) |
| 297 |
+ |
for (b = 0; b < NBLU; b++) { |
| 298 |
+ |
if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b]) |
| 299 |
+ |
addneigh(neigh, histo[r][g][b], histo[r+1][g][b]); |
| 300 |
+ |
if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b]) |
| 301 |
+ |
addneigh(neigh, histo[r][g][b], histo[r][g+1][b]); |
| 302 |
+ |
if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1]) |
| 303 |
+ |
addneigh(neigh, histo[r][g][b], histo[r][g][b+1]); |
| 304 |
+ |
} |
| 305 |
+ |
/* assign closest values */ |
| 306 |
+ |
for (r = 0; r < NRED; r++) |
| 307 |
+ |
for (g = 0; g < NGRN; g++) |
| 308 |
+ |
for (b = 0; b < NBLU; b++) |
| 309 |
+ |
setclosest(neigh, r, g, b); |
| 310 |
+ |
/* free neighbor lists */ |
| 311 |
+ |
for (i = 0; i < n; i++) |
| 312 |
+ |
free(neigh[i]); |
| 313 |
+ |
#undef i |
| 314 |
+ |
} |
| 315 |
+ |
|
| 316 |
+ |
|
| 317 |
+ |
static |
| 318 |
+ |
addneigh(nl, i, j) /* i and j are neighbors; add them to list */ |
| 319 |
+ |
register BYTE *nl[]; |
| 320 |
+ |
register int i; |
| 321 |
+ |
int j; |
| 322 |
+ |
{ |
| 323 |
+ |
int nc; |
| 324 |
+ |
char *nnl; |
| 325 |
+ |
register int t; |
| 326 |
+ |
|
| 327 |
+ |
for (nc = 0; nc < 2; nc++) { /* do both neighbors */ |
| 328 |
+ |
for (t = 0; nl[i][t] != i; t++) |
| 329 |
+ |
if (nl[i][t] == j) |
| 330 |
+ |
break; /* in list already */ |
| 331 |
+ |
if (nl[i][t] == i) { /* add to list */ |
| 332 |
+ |
nl[i][t++] = j; |
| 333 |
+ |
if (t % NBSIZ == 0) { /* enlarge list */ |
| 334 |
+ |
if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL) |
| 335 |
+ |
t--; |
| 336 |
+ |
else |
| 337 |
+ |
nl[i] = (BYTE *)nnl; |
| 338 |
+ |
} |
| 339 |
+ |
nl[i][t] = i; /* terminator */ |
| 340 |
+ |
} |
| 341 |
+ |
t = i; i = j; j = t; /* swap and do it again */ |
| 342 |
+ |
} |
| 343 |
+ |
} |
| 344 |
+ |
|
| 345 |
+ |
|
| 346 |
+ |
static unsigned |
| 347 |
+ |
dist(col, r, g, b) /* find distance from clrtab entry to r,g,b */ |
| 348 |
+ |
register BYTE col[3]; |
| 349 |
+ |
int r, g, b; |
| 350 |
+ |
{ |
| 351 |
+ |
register int tmp; |
| 352 |
+ |
register unsigned sum; |
| 353 |
+ |
|
| 354 |
+ |
tmp = col[RED]*NRED/256 - r; |
| 355 |
+ |
sum = tmp*tmp; |
| 356 |
+ |
tmp = col[GRN]*NGRN/256 - g; |
| 357 |
+ |
sum += tmp*tmp; |
| 358 |
+ |
tmp = col[BLU]*NBLU/256 - b; |
| 359 |
+ |
sum += tmp*tmp; |
| 360 |
+ |
return(sum); |
| 361 |
+ |
} |
| 362 |
+ |
|
| 363 |
+ |
|
| 364 |
+ |
static |
| 365 |
+ |
setclosest(nl, r, g, b) /* find index closest to color and assign */ |
| 366 |
+ |
BYTE *nl[]; |
| 367 |
+ |
int r, g, b; |
| 368 |
+ |
{ |
| 369 |
+ |
int ident; |
| 370 |
+ |
unsigned min; |
| 371 |
+ |
register unsigned d; |
| 372 |
+ |
register BYTE *p; |
| 373 |
+ |
/* get starting value */ |
| 374 |
+ |
min = dist(clrtab[ident=histo[r][g][b]], r, g, b); |
| 375 |
+ |
/* find minimum */ |
| 376 |
+ |
for (p = nl[ident]; *p != ident; p++) |
| 377 |
+ |
if ((d = dist(clrtab[*p], r, g, b)) < min) { |
| 378 |
+ |
min = d; |
| 379 |
+ |
histo[r][g][b] = *p; |
| 380 |
+ |
} |
| 381 |
+ |
} |
| 382 |
+ |
#endif |