1 |
– |
/* Copyright 1988 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 |
|
CLOSEST - nearest-color lookup by locally ordered search |
6 |
|
we use distance in rgb space as color metric |
12 |
|
Paul Heckbert |
13 |
|
*/ |
14 |
|
|
15 |
+ |
#include <stdlib.h> |
16 |
+ |
|
17 |
|
#include "ciq.h" |
18 |
|
|
19 |
|
#define inf 3*256*256 |
27 |
|
static char filled[512]; /* has bucket[key] been filled yet? */ |
28 |
|
static int sq[511]; |
29 |
|
|
30 |
< |
initializeclosest() { /* reset the buckets */ |
30 |
> |
static int compare(const void *a, const void *b); |
31 |
> |
static void fillbucket(int k); |
32 |
> |
|
33 |
> |
|
34 |
> |
void |
35 |
> |
initializeclosest(void) { /* reset the buckets */ |
36 |
|
int k; |
37 |
|
nfill = tests = calls = 0; |
38 |
|
for (k=0; k<512; k++) filled[k] = 0; |
39 |
|
next = space; |
40 |
|
} |
41 |
|
|
42 |
< |
closest(r,g,b) /* find pv of colormap color closest to (r,g,b) */ |
43 |
< |
int r,g,b; |
42 |
> |
int |
43 |
> |
closest( /* find pv of colormap color closest to (r,g,b) */ |
44 |
> |
int r, |
45 |
> |
int g, |
46 |
> |
int b |
47 |
> |
) |
48 |
|
{ |
49 |
|
register struct thing *p; |
50 |
|
register int *rsq,*gsq,*bsq,dist,min; |
74 |
|
|
75 |
|
#define H 16 /* half-width of a bucket */ |
76 |
|
|
77 |
< |
compare(a,b) |
78 |
< |
register struct thing *a,*b; |
77 |
> |
static int |
78 |
> |
compare( |
79 |
> |
const void *a, |
80 |
> |
const void *b |
81 |
> |
) |
82 |
|
{ |
83 |
< |
return a->mindist-b->mindist; |
83 |
> |
return ((struct thing*)a)->mindist - ((struct thing*)b)->mindist; |
84 |
|
} |
85 |
|
|
86 |
< |
fillbucket(k) /* make list of colormap colors which could be closest */ |
87 |
< |
int k; /* matches for colors in bucket #k */ |
86 |
> |
static void |
87 |
> |
fillbucket( /* make list of colormap colors which could be closest */ |
88 |
> |
int k /* matches for colors in bucket #k */ |
89 |
> |
) |
90 |
|
{ |
91 |
|
register int r,g,b,j,dist,*rsq,*gsq,*bsq,min,best; |
92 |
|
struct thing *p,*q; |
93 |
|
|
94 |
|
if (!sq[0]) for (j= -255; j<256; j++) sq[j+255] = j*j; |
95 |
|
|
96 |
< |
r = k>>1&0xe0|H; /* center of 32x32x32 cubical bucket */ |
97 |
< |
g = k<<2&0xe0|H; |
98 |
< |
b = k<<5&0xe0|H; |
96 |
> |
r = (k>>1&0xe0)|H; /* center of 32x32x32 cubical bucket */ |
97 |
> |
g = (k<<2&0xe0)|H; |
98 |
> |
b = (k<<5&0xe0)|H; |
99 |
|
rsq = sq+255-r; |
100 |
|
gsq = sq+255-g; |
101 |
|
bsq = sq+255-b; |