ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/closest.c
Revision: 2.2
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.1: +1 -4 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.2 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4 greg 1.2 /*
5 greg 1.1 CLOSEST - nearest-color lookup by locally ordered search
6     we use distance in rgb space as color metric
7    
8     fillbucket subroutine makes trimmed, sorted lists of
9     possible closest colors to speed the closest searching.
10     About 6 times faster than exhaustive.
11    
12     Paul Heckbert
13     */
14    
15     #include "ciq.h"
16    
17     #define inf 3*256*256
18 ph 1.3 #define key(r,g,b) (((r)&0xe0)<<1|((g)&0xe0)>>2|((b)&0xe0)>>5)
19     /* 9-bit hash key: rrrgggbbb */
20 greg 1.1
21 ph 1.3 static struct thing {int mindist,pv;} space[512*257],*next,*bucket[512];
22 greg 1.1 /* sorted lists of colors */
23    
24 ph 1.3 static int nfill,tests,calls;
25 greg 1.1 static char filled[512]; /* has bucket[key] been filled yet? */
26     static int sq[511];
27    
28 ph 1.3 initializeclosest() { /* reset the buckets */
29 greg 1.1 int k;
30     nfill = tests = calls = 0;
31     for (k=0; k<512; k++) filled[k] = 0;
32     next = space;
33     }
34    
35     closest(r,g,b) /* find pv of colormap color closest to (r,g,b) */
36     int r,g,b;
37     {
38     register struct thing *p;
39 ph 1.3 register int *rsq,*gsq,*bsq,dist,min;
40     int best,k;
41 greg 1.1 struct thing *p0;
42    
43 ph 1.3 k = key(r,g,b);
44     if (!filled[k]) fillbucket(k);
45 greg 1.1 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 ph 1.3 for (p0=p=bucket[k]; min>p->mindist; p++) {
52 greg 1.1 dist = rsq[color[0][p->pv]] + gsq[color[1][p->pv]] +
53     bsq[color[2][p->pv]];
54     if (dist<min) {
55     best = p->pv;
56     min = dist;
57     }
58     }
59     tests += p-p0;
60     calls++;
61     return best;
62     }
63    
64     #define H 16 /* half-width of a bucket */
65    
66     compare(a,b)
67     register struct thing *a,*b;
68     {
69     return a->mindist-b->mindist;
70     }
71    
72     fillbucket(k) /* make list of colormap colors which could be closest */
73     int k; /* matches for colors in bucket #k */
74     {
75     register int r,g,b,j,dist,*rsq,*gsq,*bsq,min,best;
76     struct thing *p,*q;
77    
78     if (!sq[0]) for (j= -255; j<256; j++) sq[j+255] = j*j;
79    
80     r = k>>1&0xe0|H; /* center of 32x32x32 cubical bucket */
81     g = k<<2&0xe0|H;
82     b = k<<5&0xe0|H;
83     rsq = sq+255-r;
84     gsq = sq+255-g;
85     bsq = sq+255-b;
86     bucket[k] = p = next;
87     min = inf;
88    
89     for (j=0; j<n; j++, p++) {
90     p->pv = j;
91     dist = 0; /* compute distance from cube surface */
92     if (color[0][j]< r-H) dist += rsq[color[0][j]+H];
93     else if (color[0][j]>=r+H) dist += rsq[color[0][j]-H+1];
94     if (color[1][j]< g-H) dist += gsq[color[1][j]+H];
95     else if (color[1][j]>=g+H) dist += gsq[color[1][j]-H+1];
96     if (color[2][j]< b-H) dist += bsq[color[2][j]+H];
97     else if (color[2][j]>=b+H) dist += bsq[color[2][j]-H+1];
98     p->mindist = dist; /* for termination test in closest */
99     dist = rsq[color[0][j]]+gsq[color[1][j]]+bsq[color[2][j]];
100     if (dist<min) { /* find color closest to cube center */
101     best = j;
102     min = dist;
103     }
104     }
105    
106     dist = rsq[color[0][best]+(color[0][best]<r?1-H:H)] +
107     gsq[color[1][best]+(color[1][best]<g?1-H:H)] +
108     bsq[color[2][best]+(color[2][best]<b?1-H:H)];
109     /* dist is an upper bound on mindist: maximum possible distance */
110     /* between a color in the bucket and its nearest neighbor */
111    
112     /* eliminate colors which couldn't be closest */
113     for (p=q=next, j=0; j<n; j++, p++)
114     if (p->mindist<dist) {
115     q->mindist = p->mindist;
116     q->pv = p->pv;
117     q++;
118     }
119     if (q<=next) fprintf(stderr,"ERROR: empty bucket!\n");
120    
121     /* and sort the list by mindist */
122     qsort(next,q-next,sizeof(struct thing),compare);
123     q->mindist = inf; /* list terminator */
124     next = q+1;
125     filled[k] = 1;
126     nfill++;
127     }
128    
129     /*endclosest() {
130     printf("filled %d buckets, averaging %d colors per list, %d of which were tested\n",
131     nfill,(next-space-nfill)/nfill,tests/calls);
132     }*/