1 |
– |
/* |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
+ |
/* |
5 |
|
CLOSEST - nearest-color lookup by locally ordered search |
6 |
|
we use distance in rgb space as color metric |
7 |
|
|
15 |
|
#include "ciq.h" |
16 |
|
|
17 |
|
#define inf 3*256*256 |
18 |
< |
#define key ((r&0xe0)<<1|(g&0xe0)>>2|(b&0xe0)>>5) /* hash key: rrrgggbbb */ |
18 |
> |
#define key(r,g,b) (((r)&0xe0)<<1|((g)&0xe0)>>2|((b)&0xe0)>>5) |
19 |
> |
/* 9-bit hash key: rrrgggbbb */ |
20 |
|
|
21 |
< |
static struct thing {int mindist,pv;} space[512*257],*next=space,*bucket[512]; |
21 |
> |
static struct thing {int mindist,pv;} space[512*257],*next,*bucket[512]; |
22 |
|
/* sorted lists of colors */ |
23 |
|
|
24 |
< |
static int nfill=0,tests=0,calls=0; |
24 |
> |
static int nfill,tests,calls; |
25 |
|
static char filled[512]; /* has bucket[key] been filled yet? */ |
26 |
|
static int sq[511]; |
27 |
|
|
28 |
< |
initialize() { /* reset the buckets */ |
28 |
> |
initializeclosest() { /* reset the buckets */ |
29 |
|
int k; |
30 |
|
nfill = tests = calls = 0; |
31 |
|
for (k=0; k<512; k++) filled[k] = 0; |
36 |
|
int r,g,b; |
37 |
|
{ |
38 |
|
register struct thing *p; |
39 |
< |
register int *rsq,*gsq,*bsq,dist,min,best; |
39 |
> |
register int *rsq,*gsq,*bsq,dist,min; |
40 |
> |
int best,k; |
41 |
|
struct thing *p0; |
42 |
|
|
43 |
< |
if (!filled[key]) fillbucket(key); |
43 |
> |
k = key(r,g,b); |
44 |
> |
if (!filled[k]) fillbucket(k); |
45 |
|
min = inf; |
46 |
|
rsq = sq+255-r; |
47 |
|
gsq = sq+255-g; |
48 |
|
bsq = sq+255-b; |
49 |
|
|
50 |
|
/* stop looking when best is closer than next could be */ |
51 |
< |
for (p0=p=bucket[key]; min>p->mindist; p++) { |
51 |
> |
for (p0=p=bucket[k]; min>p->mindist; p++) { |
52 |
|
dist = rsq[color[0][p->pv]] + gsq[color[1][p->pv]] + |
53 |
|
bsq[color[2][p->pv]]; |
54 |
|
if (dist<min) { |