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 |
– |
/* color map resolution */ |
34 |
– |
#define MAPSIZ 256 |
33 |
|
/* map a color */ |
34 |
< |
#define map_col(c,p) clrmap[ colval(c,p)<1. ? \ |
35 |
< |
(int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ] |
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 |
< |
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 */ |
50 |
|
} clrtab[1<<FBDEPTH]; |
51 |
|
/* our color correction map */ |
52 |
< |
static BYTE clrmap[MAPSIZ]; |
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)]; |
60 |
– |
/* callback for pixel assignment */ |
61 |
– |
static int (*set_pixel)(); |
59 |
|
|
60 |
|
|
61 |
|
int |
62 |
< |
new_ctab(ncolors, cset) /* start new color table with max ncolors */ |
62 |
> |
new_ctab(ncolors) /* start new color table with max ncolors */ |
63 |
|
int ncolors; |
67 |
– |
int (*cset)(); |
64 |
|
{ |
65 |
< |
if (ncolors < 1 || ncolors > 1<<FBDEPTH || cset == NULL) |
65 |
> |
if (ncolors < 1) |
66 |
|
return(0); |
67 |
< |
/* assign pixel callback routine */ |
68 |
< |
set_pixel = cset; |
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 */ |
78 |
|
|
79 |
|
|
80 |
|
int |
81 |
< |
get_pixel(col) /* get pixel for color */ |
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]; |
97 |
|
/* add to histogram */ |
98 |
|
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
99 |
|
/* find pixel in tree */ |
100 |
< |
p.t = ctree; |
104 |
< |
for (h = FBDEPTH; h > 0; h--) { |
105 |
< |
if (is_pval(*p.t)) |
106 |
< |
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 */ |
111 |
< |
} |
104 |
> |
p.t += 1<<(h+1); /* right branch */ |
105 |
|
h = pval(*p.t); |
106 |
|
/* add to color table */ |
107 |
|
p.e = clrtab + h; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
< |
make_cmap(gam) /* make gamma correction map */ |
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 < MAPSIZ; i++) |
142 |
< |
clrmap[i] = 256.0 * pow(i/(double)MAPSIZ, 1.0/gam); |
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, 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 |
|
|