27 |
|
/* minimum box count for adaptive partition */ |
28 |
|
#define MINSAMP 7 |
29 |
|
/* maximum distance^2 before color reassign */ |
30 |
< |
#define MAXDST2 5 |
30 |
> |
#define MAXDST2 12 |
31 |
|
/* maximum frame buffer depth */ |
32 |
|
#define FBDEPTH 8 |
33 |
|
/* map a color */ |
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 |
< |
struct tabent { |
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 */ |
62 |
|
new_ctab(ncolors) /* start new color table with max ncolors */ |
63 |
|
int ncolors; |
64 |
|
{ |
65 |
< |
if (ncolors < 1 || ncolors > 1<<FBDEPTH) |
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, FBDEPTH, CLRCUBE, 0, ncolors); |
72 |
> |
cut(ctree, 0, CLRCUBE, 0, ncolors); |
73 |
|
/* clear histogram */ |
74 |
|
bzero(histo, sizeof(histo)); |
75 |
|
/* return number of colors used */ |
97 |
|
/* add to histogram */ |
98 |
|
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
99 |
|
/* find pixel in tree */ |
100 |
< |
p.t = ctree; |
98 |
< |
for (h = FBDEPTH; h > 0; h--) { |
99 |
< |
if (is_pval(*p.t)) |
100 |
< |
break; |
100 |
> |
for (p.t = ctree, h = 0; is_branch(*p.t); h++) |
101 |
|
if (cv[prim(*p.t)] < part(*p.t)) |
102 |
< |
p.t++; /* left branch */ |
102 |
> |
p.t += 1<<h; /* left branch */ |
103 |
|
else |
104 |
< |
p.t += 1<<h; /* right branch */ |
105 |
< |
} |
104 |
> |
p.t += 1<<(h+1); /* right branch */ |
105 |
|
h = pval(*p.t); |
106 |
|
/* add to color table */ |
107 |
|
p.e = clrtab + h; |
155 |
|
|
156 |
|
|
157 |
|
static |
158 |
< |
cut(tree, height, box, c0, c1) /* partition color space */ |
158 |
> |
cut(tree, level, box, c0, c1) /* partition color space */ |
159 |
|
register CNODE *tree; |
160 |
< |
int height; |
160 |
> |
int level; |
161 |
|
register int box[3][2]; |
162 |
|
int c0, c1; |
163 |
|
{ |
172 |
|
bcopy(box, kb, sizeof(kb)); |
173 |
|
/* do left (lesser) branch */ |
174 |
|
kb[prim(*tree)][1] = part(*tree); |
175 |
< |
cut(tree+1, height-1, kb, c0, (c0+c1)>>1); |
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<<height), height-1, kb, (c0+c1)>>1, c1); |
179 |
> |
cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1); |
180 |
|
} |
181 |
|
|
182 |
|
|