20 |
|
|
21 |
|
#define NULL 0 |
22 |
|
/* histogram resolution */ |
23 |
< |
#define NRED 28 |
23 |
> |
#define NRED 24 |
24 |
|
#define NGRN 32 |
25 |
< |
#define NBLU 22 |
25 |
> |
#define NBLU 16 |
26 |
|
#define HMAX NGRN |
27 |
|
/* minimum box count for adaptive partition */ |
28 |
|
#define MINSAMP 7 |
29 |
< |
/* maximum frame buffer depth */ |
30 |
< |
#define FBDEPTH 8 |
31 |
< |
/* color map resolution */ |
32 |
< |
#define MAPSIZ 128 |
29 |
> |
/* maximum distance^2 before color reassign */ |
30 |
> |
#define MAXDST2 12 |
31 |
|
/* map a color */ |
32 |
|
#define map_col(c,p) clrmap[p][ colval(c,p)<1. ? \ |
33 |
< |
(int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ] |
33 |
> |
(int)(colval(c,p)*256.) : 255 ] |
34 |
|
/* color partition tree */ |
35 |
|
#define CNODE short |
36 |
|
#define set_branch(p,c) ((c)<<2|(p)) |
37 |
|
#define set_pval(pv) ((pv)<<2|3) |
38 |
+ |
#define is_branch(cn) (((cn)&3)!=3) |
39 |
|
#define is_pval(cn) (((cn)&3)==3) |
40 |
|
#define part(cn) ((cn)>>2) |
41 |
|
#define prim(cn) ((cn)&3) |
42 |
|
#define pval(cn) ((cn)>>2) |
43 |
|
/* our color table */ |
44 |
< |
COLR clrtab[1<<FBDEPTH]; |
44 |
> |
static struct tabent { |
45 |
> |
long sum[3]; /* sum of colors using this entry */ |
46 |
> |
long n; /* number of colors */ |
47 |
> |
short ent[3]; /* current table value */ |
48 |
> |
} *clrtab = NULL; |
49 |
> |
/* color cube partition */ |
50 |
> |
static CNODE *ctree = NULL; |
51 |
|
/* our color correction map */ |
52 |
< |
static BYTE clrmap[3][MAPSIZ]; |
52 |
> |
static BYTE clrmap[3][256]; |
53 |
|
/* histogram of colors used */ |
54 |
< |
static unsigned histo[NRED][NGRN][NBLU]; |
55 |
< |
/* initial color cube boundaries */ |
54 |
> |
static unsigned short histo[NRED][NGRN][NBLU]; |
55 |
> |
/* initial color cube boundary */ |
56 |
|
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
52 |
– |
/* color cube partition */ |
53 |
– |
static CNODE ctree[1<<(FBDEPTH+1)]; |
57 |
|
|
58 |
|
|
59 |
< |
COLR * |
60 |
< |
get_ctab(ncolors) /* assign a color table with max ncolors */ |
59 |
> |
int |
60 |
> |
new_ctab(ncolors) /* start new color table with max ncolors */ |
61 |
|
int ncolors; |
62 |
|
{ |
63 |
< |
if (ncolors < 1 || ncolors > 1<<FBDEPTH) |
64 |
< |
return(NULL); |
65 |
< |
/* partition color table */ |
66 |
< |
cut(ctree, FBDEPTH, CLRCUBE, 0, ncolors); |
63 |
> |
int treesize; |
64 |
> |
|
65 |
> |
if (ncolors < 1) |
66 |
> |
return(0); |
67 |
> |
/* free old tables */ |
68 |
> |
if (clrtab != NULL) |
69 |
> |
free((char *)clrtab); |
70 |
> |
if (ctree != NULL) |
71 |
> |
free((char *)ctree); |
72 |
> |
/* get new tables */ |
73 |
> |
for (treesize = 1; treesize < ncolors; treesize <<= 1) |
74 |
> |
; |
75 |
> |
treesize <<= 1; |
76 |
> |
clrtab = (struct tabent *)calloc(ncolors, sizeof(struct tabent)); |
77 |
> |
ctree = (CNODE *)malloc(treesize*sizeof(CNODE)); |
78 |
> |
if (clrtab == NULL || ctree == NULL) |
79 |
> |
return(0); |
80 |
> |
/* partition color space */ |
81 |
> |
cut(ctree, 0, CLRCUBE, 0, ncolors); |
82 |
|
/* clear histogram */ |
83 |
< |
bzero(histo, sizeof(histo)); |
84 |
< |
/* return color table */ |
85 |
< |
return(clrtab); |
83 |
> |
bzero((char *)histo, sizeof(histo)); |
84 |
> |
/* return number of colors used */ |
85 |
> |
return(ncolors); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
int |
90 |
< |
get_pixel(col) /* get pixel for color */ |
90 |
> |
get_pixel(col, set_pixel) /* get pixel for color */ |
91 |
|
COLOR col; |
92 |
+ |
int (*set_pixel)(); |
93 |
|
{ |
94 |
+ |
extern char errmsg[]; |
95 |
+ |
int r, g, b; |
96 |
|
int cv[3]; |
97 |
|
register CNODE *tp; |
98 |
|
register int h; |
99 |
< |
/* map color */ |
100 |
< |
cv[RED] = map_col(col,RED); |
101 |
< |
cv[GRN] = map_col(col,GRN); |
102 |
< |
cv[BLU] = map_col(col,BLU); |
103 |
< |
/* add to histogram */ |
99 |
> |
/* map color */ |
100 |
> |
r = map_col(col,RED); |
101 |
> |
g = map_col(col,GRN); |
102 |
> |
b = map_col(col,BLU); |
103 |
> |
/* reduce resolution */ |
104 |
> |
cv[RED] = (r*NRED)>>8; |
105 |
> |
cv[GRN] = (g*NGRN)>>8; |
106 |
> |
cv[BLU] = (b*NBLU)>>8; |
107 |
> |
/* add to histogram */ |
108 |
|
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
109 |
< |
/* find pixel value in tree */ |
110 |
< |
tp = ctree; |
86 |
< |
for (h = FBDEPTH; h > 0; h--) { |
87 |
< |
if (is_pval(*tp)) |
88 |
< |
break; |
109 |
> |
/* find pixel in tree */ |
110 |
> |
for (tp = ctree, h = 0; is_branch(*tp); h++) |
111 |
|
if (cv[prim(*tp)] < part(*tp)) |
112 |
< |
tp++; /* left branch */ |
112 |
> |
tp += 1<<h; /* left branch */ |
113 |
|
else |
114 |
< |
tp += 1<<h; /* right branch */ |
115 |
< |
} |
116 |
< |
#ifdef notdef |
117 |
< |
printf("distance (%d,%d,%d)\n", |
118 |
< |
cv[RED] - clrtab[pval(*tp)][RED]*NRED/256, |
119 |
< |
cv[GRN] - clrtab[pval(*tp)][GRN]*NGRN/256, |
120 |
< |
cv[BLU] - clrtab[pval(*tp)][BLU]*NBLU/256); |
114 |
> |
tp += 1<<(h+1); /* right branch */ |
115 |
> |
h = pval(*tp); |
116 |
> |
/* add to color table */ |
117 |
> |
clrtab[h].sum[RED] += r; |
118 |
> |
clrtab[h].sum[GRN] += g; |
119 |
> |
clrtab[h].sum[BLU] += b; |
120 |
> |
clrtab[h].n++; |
121 |
> |
/* recompute average */ |
122 |
> |
r = clrtab[h].sum[RED] / clrtab[h].n; |
123 |
> |
g = clrtab[h].sum[GRN] / clrtab[h].n; |
124 |
> |
b = clrtab[h].sum[BLU] / clrtab[h].n; |
125 |
> |
/* check for movement */ |
126 |
> |
if (clrtab[h].n == 1 || |
127 |
> |
(r-clrtab[h].ent[RED])*(r-clrtab[h].ent[RED]) + |
128 |
> |
(g-clrtab[h].ent[GRN])*(g-clrtab[h].ent[GRN]) + |
129 |
> |
(b-clrtab[h].ent[BLU])*(b-clrtab[h].ent[BLU]) > MAXDST2) { |
130 |
> |
clrtab[h].ent[RED] = r; |
131 |
> |
clrtab[h].ent[GRN] = g; /* reassign pixel */ |
132 |
> |
clrtab[h].ent[BLU] = b; |
133 |
> |
#ifdef DEBUG |
134 |
> |
sprintf(errmsg, "pixel %d = (%d,%d,%d) (%d refs)\n", |
135 |
> |
h, r, g, b, clrtab[h].n); |
136 |
> |
eputs(errmsg); |
137 |
|
#endif |
138 |
< |
return(pval(*tp)); |
138 |
> |
(*set_pixel)(h, r, g, b); |
139 |
> |
} |
140 |
> |
return(h); /* return pixel value */ |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
make_cmap(gam) /* make gamma correction map */ |
144 |
> |
make_gmap(gam) /* make gamma correction map */ |
145 |
|
double gam; |
146 |
|
{ |
147 |
|
extern double pow(); |
108 |
– |
double d; |
148 |
|
register int i; |
149 |
|
|
150 |
< |
for (i = 0; i < MAPSIZ; i++) { |
151 |
< |
d = pow(i/(double)MAPSIZ, 1.0/gam); |
152 |
< |
clrmap[RED][i] = d * NRED; |
153 |
< |
clrmap[GRN][i] = d * NGRN; |
115 |
< |
clrmap[BLU][i] = d * NBLU; |
116 |
< |
} |
150 |
> |
for (i = 0; i < 256; i++) |
151 |
> |
clrmap[RED][i] = |
152 |
> |
clrmap[GRN][i] = |
153 |
> |
clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
+ |
set_cmap(rmap, gmap, bmap) /* set custom color correction map */ |
158 |
+ |
BYTE *rmap, *gmap, *bmap; |
159 |
+ |
{ |
160 |
+ |
bcopy((char *)rmap, (char *)clrmap[RED], 256); |
161 |
+ |
bcopy((char *)gmap, (char *)clrmap[GRN], 256); |
162 |
+ |
bcopy((char *)bmap, (char *)clrmap[BLU], 256); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
|
166 |
+ |
map_color(rgb, col) /* map a color to a byte triplet */ |
167 |
+ |
BYTE rgb[3]; |
168 |
+ |
COLOR col; |
169 |
+ |
{ |
170 |
+ |
rgb[RED] = map_col(col,RED); |
171 |
+ |
rgb[GRN] = map_col(col,GRN); |
172 |
+ |
rgb[BLU] = map_col(col,BLU); |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
|
176 |
|
static |
177 |
< |
cut(tree, height, box, c0, c1) /* partition color space */ |
177 |
> |
cut(tree, level, box, c0, c1) /* partition color space */ |
178 |
|
register CNODE *tree; |
179 |
< |
int height; |
179 |
> |
int level; |
180 |
|
register int box[3][2]; |
181 |
|
int c0, c1; |
182 |
|
{ |
183 |
|
int kb[3][2]; |
184 |
|
|
185 |
< |
if (c1-c0 == 1) { /* assign color */ |
130 |
< |
clrtab[c0][RED] = ((box[RED][0]+box[RED][1])<<7)/NRED; |
131 |
< |
clrtab[c0][GRN] = ((box[GRN][0]+box[GRN][1])<<7)/NGRN; |
132 |
< |
clrtab[c0][BLU] = ((box[BLU][0]+box[BLU][1])<<7)/NBLU; |
133 |
< |
clrtab[c0][EXP] = COLXS; |
185 |
> |
if (c1-c0 <= 1) { /* assign pixel */ |
186 |
|
*tree = set_pval(c0); |
135 |
– |
#ifdef notdef |
136 |
– |
printf("final box size = (%d,%d,%d)\n", |
137 |
– |
box[RED][1] - box[RED][0], |
138 |
– |
box[GRN][1] - box[GRN][0], |
139 |
– |
box[BLU][1] - box[BLU][0]); |
140 |
– |
#endif |
187 |
|
return; |
188 |
|
} |
189 |
|
/* split box */ |
190 |
|
*tree = split(box); |
191 |
< |
bcopy(box, kb, sizeof(kb)); |
191 |
> |
bcopy((char *)box, (char *)kb, sizeof(kb)); |
192 |
> |
/* do left (lesser) branch */ |
193 |
|
kb[prim(*tree)][1] = part(*tree); |
194 |
< |
cut(tree+1, height-1, kb, c0, (c0+c1)>>1); /* lesser */ |
194 |
> |
cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1); |
195 |
> |
/* do right branch */ |
196 |
|
kb[prim(*tree)][0] = part(*tree); |
197 |
|
kb[prim(*tree)][1] = box[prim(*tree)][1]; |
198 |
< |
cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1); /* greater */ |
198 |
> |
cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1); |
199 |
|
} |
200 |
|
|
201 |
|
|