1 |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* colortab.c - allocate and control dynamic color table. |
9 |
* |
10 |
* We start off with a uniform partition of color space. |
11 |
* As pixels are sent to the frame buffer, a histogram is built. |
12 |
* When a new color table is requested, the histogram is used |
13 |
* to make a pseudo-optimal partition, after which the |
14 |
* histogram is cleared. This algorithm |
15 |
* performs only as well as the next drawing's color |
16 |
* distribution is correlated to the last. |
17 |
*/ |
18 |
|
19 |
#include "color.h" |
20 |
|
21 |
#define NULL 0 |
22 |
/* histogram resolution */ |
23 |
#define NRED 24 |
24 |
#define NGRN 32 |
25 |
#define NBLU 16 |
26 |
#define HMAX NGRN |
27 |
/* minimum box count for adaptive partition */ |
28 |
#define MINSAMP 7 |
29 |
/* maximum distance^2 before color reassign */ |
30 |
#define MAXDST2 12 |
31 |
/* maximum frame buffer depth */ |
32 |
#define FBDEPTH 8 |
33 |
/* map a color */ |
34 |
#define map_col(c,p) clrmap[p][ colval(c,p)<1. ? \ |
35 |
(int)(colval(c,p)*256.) : 255 ] |
36 |
/* color partition tree */ |
37 |
#define CNODE short |
38 |
#define set_branch(p,c) ((c)<<2|(p)) |
39 |
#define set_pval(pv) ((pv)<<2|3) |
40 |
#define is_branch(cn) (((cn)&3)!=3) |
41 |
#define is_pval(cn) (((cn)&3)==3) |
42 |
#define part(cn) ((cn)>>2) |
43 |
#define prim(cn) ((cn)&3) |
44 |
#define pval(cn) ((cn)>>2) |
45 |
/* our color table */ |
46 |
static struct tabent { |
47 |
long sum[3]; /* sum of colors using this entry */ |
48 |
long n; /* number of colors */ |
49 |
short ent[3]; /* current table value */ |
50 |
} clrtab[1<<FBDEPTH]; |
51 |
/* our color correction map */ |
52 |
static BYTE clrmap[3][256]; |
53 |
/* histogram of colors used */ |
54 |
static unsigned histo[NRED][NGRN][NBLU]; |
55 |
/* initial color cube boundaries */ |
56 |
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
57 |
/* color cube partition */ |
58 |
static CNODE ctree[1<<(FBDEPTH+1)]; |
59 |
|
60 |
|
61 |
int |
62 |
new_ctab(ncolors) /* start new color table with max ncolors */ |
63 |
int ncolors; |
64 |
{ |
65 |
if (ncolors < 1) |
66 |
return(0); |
67 |
if (ncolors > 1<<FBDEPTH) |
68 |
ncolors = 1<<FBDEPTH; |
69 |
/* clear color table */ |
70 |
bzero(clrtab, sizeof(clrtab)); |
71 |
/* partition color space */ |
72 |
cut(ctree, 0, CLRCUBE, 0, ncolors); |
73 |
/* clear histogram */ |
74 |
bzero(histo, sizeof(histo)); |
75 |
/* return number of colors used */ |
76 |
return(ncolors); |
77 |
} |
78 |
|
79 |
|
80 |
int |
81 |
get_pixel(col, set_pixel) /* get pixel for color */ |
82 |
COLOR col; |
83 |
int (*set_pixel)(); |
84 |
{ |
85 |
int r, g, b; |
86 |
int cv[3]; |
87 |
register union { CNODE *t; struct tabent *e; } p; |
88 |
register int h; |
89 |
/* map color */ |
90 |
r = map_col(col,RED); |
91 |
g = map_col(col,GRN); |
92 |
b = map_col(col,BLU); |
93 |
/* reduce resolution */ |
94 |
cv[RED] = (r*NRED)>>8; |
95 |
cv[GRN] = (g*NGRN)>>8; |
96 |
cv[BLU] = (b*NBLU)>>8; |
97 |
/* add to histogram */ |
98 |
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
99 |
/* find pixel in tree */ |
100 |
for (p.t = ctree, h = 0; is_branch(*p.t); h++) |
101 |
if (cv[prim(*p.t)] < part(*p.t)) |
102 |
p.t += 1<<h; /* left branch */ |
103 |
else |
104 |
p.t += 1<<(h+1); /* right branch */ |
105 |
h = pval(*p.t); |
106 |
/* add to color table */ |
107 |
p.e = clrtab + h; |
108 |
/* add to sum */ |
109 |
p.e->sum[RED] += r; |
110 |
p.e->sum[GRN] += g; |
111 |
p.e->sum[BLU] += b; |
112 |
p.e->n++; |
113 |
/* recompute average */ |
114 |
r = p.e->sum[RED] / p.e->n; |
115 |
g = p.e->sum[GRN] / p.e->n; |
116 |
b = p.e->sum[BLU] / p.e->n; |
117 |
/* check for movement */ |
118 |
if (p.e->n == 1 || |
119 |
(r-p.e->ent[RED])*(r-p.e->ent[RED]) + |
120 |
(g-p.e->ent[GRN])*(g-p.e->ent[GRN]) + |
121 |
(b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) { |
122 |
p.e->ent[RED] = r; |
123 |
p.e->ent[GRN] = g; /* reassign pixel */ |
124 |
p.e->ent[BLU] = b; |
125 |
#ifdef notdef |
126 |
printf("pixel %d = (%d,%d,%d) (%d refs)\n", |
127 |
h, r, g, b, p.e->n); |
128 |
#endif |
129 |
(*set_pixel)(h, r, g, b); |
130 |
} |
131 |
return(h); /* return pixel value */ |
132 |
} |
133 |
|
134 |
|
135 |
make_gmap(gam) /* make gamma correction map */ |
136 |
double gam; |
137 |
{ |
138 |
extern double pow(); |
139 |
register int i; |
140 |
|
141 |
for (i = 0; i < 256; i++) |
142 |
clrmap[RED][i] = |
143 |
clrmap[GRN][i] = |
144 |
clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam); |
145 |
} |
146 |
|
147 |
|
148 |
set_cmap(rmap, gmap, bmap) /* set custom color correction map */ |
149 |
BYTE *rmap, *gmap, *bmap; |
150 |
{ |
151 |
bcopy(rmap, clrmap[RED], 256); |
152 |
bcopy(gmap, clrmap[GRN], 256); |
153 |
bcopy(bmap, clrmap[BLU], 256); |
154 |
} |
155 |
|
156 |
|
157 |
static |
158 |
cut(tree, level, box, c0, c1) /* partition color space */ |
159 |
register CNODE *tree; |
160 |
int level; |
161 |
register int box[3][2]; |
162 |
int c0, c1; |
163 |
{ |
164 |
int kb[3][2]; |
165 |
|
166 |
if (c1-c0 <= 1) { /* assign pixel */ |
167 |
*tree = set_pval(c0); |
168 |
return; |
169 |
} |
170 |
/* split box */ |
171 |
*tree = split(box); |
172 |
bcopy(box, kb, sizeof(kb)); |
173 |
/* do left (lesser) branch */ |
174 |
kb[prim(*tree)][1] = part(*tree); |
175 |
cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1); |
176 |
/* do right branch */ |
177 |
kb[prim(*tree)][0] = part(*tree); |
178 |
kb[prim(*tree)][1] = box[prim(*tree)][1]; |
179 |
cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1); |
180 |
} |
181 |
|
182 |
|
183 |
static int |
184 |
split(box) /* find median cut for box */ |
185 |
register int box[3][2]; |
186 |
{ |
187 |
#define c0 r |
188 |
register int r, g, b; |
189 |
int pri; |
190 |
int t[HMAX], med; |
191 |
/* find dominant axis */ |
192 |
pri = RED; |
193 |
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |
194 |
pri = GRN; |
195 |
if (box[BLU][1]-box[BLU][0] > box[pri][1]-box[pri][0]) |
196 |
pri = BLU; |
197 |
/* sum histogram over box */ |
198 |
med = 0; |
199 |
switch (pri) { |
200 |
case RED: |
201 |
for (r = box[RED][0]; r < box[RED][1]; r++) { |
202 |
t[r] = 0; |
203 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) |
204 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) |
205 |
t[r] += histo[r][g][b]; |
206 |
med += t[r]; |
207 |
} |
208 |
break; |
209 |
case GRN: |
210 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) { |
211 |
t[g] = 0; |
212 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) |
213 |
for (r = box[RED][0]; r < box[RED][1]; r++) |
214 |
t[g] += histo[r][g][b]; |
215 |
med += t[g]; |
216 |
} |
217 |
break; |
218 |
case BLU: |
219 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) { |
220 |
t[b] = 0; |
221 |
for (r = box[RED][0]; r < box[RED][1]; r++) |
222 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) |
223 |
t[b] += histo[r][g][b]; |
224 |
med += t[b]; |
225 |
} |
226 |
break; |
227 |
} |
228 |
if (med < MINSAMP) /* if too sparse, split at midpoint */ |
229 |
return(set_branch(pri,(box[pri][0]+box[pri][1])>>1)); |
230 |
/* find median position */ |
231 |
med >>= 1; |
232 |
for (c0 = box[pri][0]; med > 0; c0++) |
233 |
med -= t[c0]; |
234 |
if (c0 > (box[pri][0]+box[pri][1])>>1) /* if past the midpoint */ |
235 |
c0--; /* part left of median */ |
236 |
return(set_branch(pri,c0)); |
237 |
#undef c0 |
238 |
} |