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_pval(cn) (((cn)&3)==3) |
41 |
#define part(cn) ((cn)>>2) |
42 |
#define prim(cn) ((cn)&3) |
43 |
#define pval(cn) ((cn)>>2) |
44 |
/* our color table */ |
45 |
static struct tabent { |
46 |
long sum[3]; /* sum of colors using this entry */ |
47 |
long n; /* number of colors */ |
48 |
short ent[3]; /* current table value */ |
49 |
} clrtab[1<<FBDEPTH]; |
50 |
/* our color correction map */ |
51 |
static BYTE clrmap[3][256]; |
52 |
/* histogram of colors used */ |
53 |
static unsigned histo[NRED][NGRN][NBLU]; |
54 |
/* initial color cube boundaries */ |
55 |
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
56 |
/* color cube partition */ |
57 |
static CNODE ctree[1<<(FBDEPTH+1)]; |
58 |
|
59 |
|
60 |
int |
61 |
new_ctab(ncolors) /* start new color table with max ncolors */ |
62 |
int ncolors; |
63 |
{ |
64 |
if (ncolors < 1) |
65 |
return(0); |
66 |
if (ncolors > 1<<FBDEPTH) |
67 |
ncolors = 1<<FBDEPTH; |
68 |
/* clear color table */ |
69 |
bzero(clrtab, sizeof(clrtab)); |
70 |
/* partition color space */ |
71 |
cut(ctree, FBDEPTH, CLRCUBE, 0, ncolors); |
72 |
/* clear histogram */ |
73 |
bzero(histo, sizeof(histo)); |
74 |
/* return number of colors used */ |
75 |
return(ncolors); |
76 |
} |
77 |
|
78 |
|
79 |
int |
80 |
get_pixel(col, set_pixel) /* get pixel for color */ |
81 |
COLOR col; |
82 |
int (*set_pixel)(); |
83 |
{ |
84 |
int r, g, b; |
85 |
int cv[3]; |
86 |
register union { CNODE *t; struct tabent *e; } p; |
87 |
register int h; |
88 |
/* map color */ |
89 |
r = map_col(col,RED); |
90 |
g = map_col(col,GRN); |
91 |
b = map_col(col,BLU); |
92 |
/* reduce resolution */ |
93 |
cv[RED] = (r*NRED)>>8; |
94 |
cv[GRN] = (g*NGRN)>>8; |
95 |
cv[BLU] = (b*NBLU)>>8; |
96 |
/* add to histogram */ |
97 |
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
98 |
/* find pixel in tree */ |
99 |
p.t = ctree; |
100 |
for (h = FBDEPTH; h > 0; h--) { |
101 |
if (is_pval(*p.t)) |
102 |
break; |
103 |
if (cv[prim(*p.t)] < part(*p.t)) |
104 |
p.t++; /* left branch */ |
105 |
else |
106 |
p.t += 1<<h; /* right branch */ |
107 |
} |
108 |
h = pval(*p.t); |
109 |
/* add to color table */ |
110 |
p.e = clrtab + h; |
111 |
/* add to sum */ |
112 |
p.e->sum[RED] += r; |
113 |
p.e->sum[GRN] += g; |
114 |
p.e->sum[BLU] += b; |
115 |
p.e->n++; |
116 |
/* recompute average */ |
117 |
r = p.e->sum[RED] / p.e->n; |
118 |
g = p.e->sum[GRN] / p.e->n; |
119 |
b = p.e->sum[BLU] / p.e->n; |
120 |
/* check for movement */ |
121 |
if (p.e->n == 1 || |
122 |
(r-p.e->ent[RED])*(r-p.e->ent[RED]) + |
123 |
(g-p.e->ent[GRN])*(g-p.e->ent[GRN]) + |
124 |
(b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) { |
125 |
p.e->ent[RED] = r; |
126 |
p.e->ent[GRN] = g; /* reassign pixel */ |
127 |
p.e->ent[BLU] = b; |
128 |
#ifdef notdef |
129 |
printf("pixel %d = (%d,%d,%d) (%d refs)\n", |
130 |
h, r, g, b, p.e->n); |
131 |
#endif |
132 |
(*set_pixel)(h, r, g, b); |
133 |
} |
134 |
return(h); /* return pixel value */ |
135 |
} |
136 |
|
137 |
|
138 |
make_gmap(gam) /* make gamma correction map */ |
139 |
double gam; |
140 |
{ |
141 |
extern double pow(); |
142 |
register int i; |
143 |
|
144 |
for (i = 0; i < 256; i++) |
145 |
clrmap[RED][i] = |
146 |
clrmap[GRN][i] = |
147 |
clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam); |
148 |
} |
149 |
|
150 |
|
151 |
set_cmap(rmap, gmap, bmap) /* set custom color correction map */ |
152 |
BYTE *rmap, *gmap, *bmap; |
153 |
{ |
154 |
bcopy(rmap, clrmap[RED], 256); |
155 |
bcopy(gmap, clrmap[GRN], 256); |
156 |
bcopy(bmap, clrmap[BLU], 256); |
157 |
} |
158 |
|
159 |
|
160 |
static |
161 |
cut(tree, height, box, c0, c1) /* partition color space */ |
162 |
register CNODE *tree; |
163 |
int height; |
164 |
register int box[3][2]; |
165 |
int c0, c1; |
166 |
{ |
167 |
int kb[3][2]; |
168 |
|
169 |
if (c1-c0 <= 1) { /* assign pixel */ |
170 |
*tree = set_pval(c0); |
171 |
return; |
172 |
} |
173 |
/* split box */ |
174 |
*tree = split(box); |
175 |
bcopy(box, kb, sizeof(kb)); |
176 |
/* do left (lesser) branch */ |
177 |
kb[prim(*tree)][1] = part(*tree); |
178 |
cut(tree+1, height-1, kb, c0, (c0+c1)>>1); |
179 |
/* do right branch */ |
180 |
kb[prim(*tree)][0] = part(*tree); |
181 |
kb[prim(*tree)][1] = box[prim(*tree)][1]; |
182 |
cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1); |
183 |
} |
184 |
|
185 |
|
186 |
static int |
187 |
split(box) /* find median cut for box */ |
188 |
register int box[3][2]; |
189 |
{ |
190 |
#define c0 r |
191 |
register int r, g, b; |
192 |
int pri; |
193 |
int t[HMAX], med; |
194 |
/* find dominant axis */ |
195 |
pri = RED; |
196 |
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |
197 |
pri = GRN; |
198 |
if (box[BLU][1]-box[BLU][0] > box[pri][1]-box[pri][0]) |
199 |
pri = BLU; |
200 |
/* sum histogram over box */ |
201 |
med = 0; |
202 |
switch (pri) { |
203 |
case RED: |
204 |
for (r = box[RED][0]; r < box[RED][1]; r++) { |
205 |
t[r] = 0; |
206 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) |
207 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) |
208 |
t[r] += histo[r][g][b]; |
209 |
med += t[r]; |
210 |
} |
211 |
break; |
212 |
case GRN: |
213 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) { |
214 |
t[g] = 0; |
215 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) |
216 |
for (r = box[RED][0]; r < box[RED][1]; r++) |
217 |
t[g] += histo[r][g][b]; |
218 |
med += t[g]; |
219 |
} |
220 |
break; |
221 |
case BLU: |
222 |
for (b = box[BLU][0]; b < box[BLU][1]; b++) { |
223 |
t[b] = 0; |
224 |
for (r = box[RED][0]; r < box[RED][1]; r++) |
225 |
for (g = box[GRN][0]; g < box[GRN][1]; g++) |
226 |
t[b] += histo[r][g][b]; |
227 |
med += t[b]; |
228 |
} |
229 |
break; |
230 |
} |
231 |
if (med < MINSAMP) /* if too sparse, split at midpoint */ |
232 |
return(set_branch(pri,(box[pri][0]+box[pri][1])>>1)); |
233 |
/* find median position */ |
234 |
med >>= 1; |
235 |
for (c0 = box[pri][0]; med > 0; c0++) |
236 |
med -= t[c0]; |
237 |
if (c0 > (box[pri][0]+box[pri][1])>>1) /* if past the midpoint */ |
238 |
c0--; /* part left of median */ |
239 |
return(set_branch(pri,c0)); |
240 |
#undef c0 |
241 |
} |