27 |
|
/* minimum box count for adaptive partition */ |
28 |
|
#define MINSAMP 7 |
29 |
|
/* maximum distance^2 before color reassign */ |
30 |
< |
#define MAXDST2 5 |
31 |
< |
/* maximum frame buffer depth */ |
32 |
< |
#define FBDEPTH 8 |
33 |
< |
/* color map resolution */ |
34 |
< |
#define MAPSIZ 256 |
30 |
> |
#define MAXDST2 12 |
31 |
|
/* map a color */ |
32 |
< |
#define map_col(c,p) clrmap[ colval(c,p)<1. ? \ |
33 |
< |
(int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ] |
32 |
> |
#define map_col(c,p) clrmap[p][ colval(c,p)<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 |
< |
struct tabent { |
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[1<<FBDEPTH]; |
48 |
> |
} *clrtab = NULL; |
49 |
> |
/* color cube partition */ |
50 |
> |
static CNODE *ctree = NULL; |
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 */ |
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}; |
58 |
– |
/* color cube partition */ |
59 |
– |
static CNODE ctree[1<<(FBDEPTH+1)]; |
60 |
– |
/* callback for pixel assignment */ |
61 |
– |
static int (*set_pixel)(); |
57 |
|
|
58 |
|
|
59 |
|
int |
60 |
< |
new_ctab(ncolors, cset) /* start new color table with max ncolors */ |
60 |
> |
new_ctab(ncolors) /* start new color table with max ncolors */ |
61 |
|
int ncolors; |
67 |
– |
int (*cset)(); |
62 |
|
{ |
63 |
< |
if (ncolors < 1 || ncolors > 1<<FBDEPTH || cset == NULL) |
63 |
> |
int treesize; |
64 |
> |
|
65 |
> |
if (ncolors < 1) |
66 |
|
return(0); |
67 |
< |
/* assign pixel callback routine */ |
68 |
< |
set_pixel = cset; |
69 |
< |
/* clear color table */ |
70 |
< |
bzero(clrtab, sizeof(clrtab)); |
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, FBDEPTH, CLRCUBE, 0, ncolors); |
81 |
> |
cut(ctree, 0, CLRCUBE, 0, ncolors); |
82 |
|
/* clear histogram */ |
83 |
|
bzero(histo, sizeof(histo)); |
84 |
|
/* return number of colors used */ |
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 |
|
int r, g, b; |
95 |
|
int cv[3]; |
106 |
|
/* add to histogram */ |
107 |
|
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
108 |
|
/* find pixel in tree */ |
109 |
< |
p.t = ctree; |
104 |
< |
for (h = FBDEPTH; h > 0; h--) { |
105 |
< |
if (is_pval(*p.t)) |
106 |
< |
break; |
109 |
> |
for (p.t = ctree, h = 0; is_branch(*p.t); h++) |
110 |
|
if (cv[prim(*p.t)] < part(*p.t)) |
111 |
< |
p.t++; /* left branch */ |
111 |
> |
p.t += 1<<h; /* left branch */ |
112 |
|
else |
113 |
< |
p.t += 1<<h; /* right branch */ |
111 |
< |
} |
113 |
> |
p.t += 1<<(h+1); /* right branch */ |
114 |
|
h = pval(*p.t); |
115 |
|
/* add to color table */ |
116 |
|
p.e = clrtab + h; |
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(); |
148 |
|
register int i; |
149 |
|
|
150 |
< |
for (i = 0; i < MAPSIZ; i++) |
151 |
< |
clrmap[i] = 256.0 * pow(i/(double)MAPSIZ, 1.0/gam); |
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(rmap, clrmap[RED], 256); |
161 |
+ |
bcopy(gmap, clrmap[GRN], 256); |
162 |
+ |
bcopy(bmap, clrmap[BLU], 256); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
|
166 |
|
static |
167 |
< |
cut(tree, height, box, c0, c1) /* partition color space */ |
167 |
> |
cut(tree, level, box, c0, c1) /* partition color space */ |
168 |
|
register CNODE *tree; |
169 |
< |
int height; |
169 |
> |
int level; |
170 |
|
register int box[3][2]; |
171 |
|
int c0, c1; |
172 |
|
{ |
181 |
|
bcopy(box, kb, sizeof(kb)); |
182 |
|
/* do left (lesser) branch */ |
183 |
|
kb[prim(*tree)][1] = part(*tree); |
184 |
< |
cut(tree+1, height-1, kb, c0, (c0+c1)>>1); |
184 |
> |
cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1); |
185 |
|
/* do right branch */ |
186 |
|
kb[prim(*tree)][0] = part(*tree); |
187 |
|
kb[prim(*tree)][1] = box[prim(*tree)][1]; |
188 |
< |
cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1); |
188 |
> |
cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1); |
189 |
|
} |
190 |
|
|
191 |
|
|