28 |
|
#define MINSAMP 7 |
29 |
|
/* maximum distance^2 before color reassign */ |
30 |
|
#define MAXDST2 12 |
31 |
– |
/* maximum frame buffer depth */ |
32 |
– |
#define FBDEPTH 8 |
31 |
|
/* map a color */ |
32 |
|
#define map_col(c,p) clrmap[p][ colval(c,p)<1. ? \ |
33 |
|
(int)(colval(c,p)*256.) : 255 ] |
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[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}; |
57 |
– |
/* color cube partition */ |
58 |
– |
static CNODE ctree[1<<(FBDEPTH+1)]; |
57 |
|
|
58 |
|
|
59 |
|
int |
60 |
|
new_ctab(ncolors) /* start new color table with max ncolors */ |
61 |
|
int ncolors; |
62 |
|
{ |
63 |
+ |
int treesize; |
64 |
+ |
|
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)); |
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)); |
83 |
> |
bzero((char *)histo, sizeof(histo)); |
84 |
|
/* return number of colors used */ |
85 |
|
return(ncolors); |
86 |
|
} |
93 |
|
{ |
94 |
|
int r, g, b; |
95 |
|
int cv[3]; |
96 |
< |
register union { CNODE *t; struct tabent *e; } p; |
96 |
> |
register CNODE *tp; |
97 |
|
register int h; |
98 |
|
/* map color */ |
99 |
|
r = map_col(col,RED); |
106 |
|
/* add to histogram */ |
107 |
|
histo[cv[RED]][cv[GRN]][cv[BLU]]++; |
108 |
|
/* find pixel in tree */ |
109 |
< |
for (p.t = ctree, h = 0; is_branch(*p.t); h++) |
110 |
< |
if (cv[prim(*p.t)] < part(*p.t)) |
111 |
< |
p.t += 1<<h; /* left branch */ |
109 |
> |
for (tp = ctree, h = 0; is_branch(*tp); h++) |
110 |
> |
if (cv[prim(*tp)] < part(*tp)) |
111 |
> |
tp += 1<<h; /* left branch */ |
112 |
|
else |
113 |
< |
p.t += 1<<(h+1); /* right branch */ |
114 |
< |
h = pval(*p.t); |
113 |
> |
tp += 1<<(h+1); /* right branch */ |
114 |
> |
h = pval(*tp); |
115 |
|
/* add to color table */ |
116 |
< |
p.e = clrtab + h; |
117 |
< |
/* add to sum */ |
118 |
< |
p.e->sum[RED] += r; |
119 |
< |
p.e->sum[GRN] += g; |
111 |
< |
p.e->sum[BLU] += b; |
112 |
< |
p.e->n++; |
116 |
> |
clrtab[h].sum[RED] += r; |
117 |
> |
clrtab[h].sum[GRN] += g; |
118 |
> |
clrtab[h].sum[BLU] += b; |
119 |
> |
clrtab[h].n++; |
120 |
|
/* recompute average */ |
121 |
< |
r = p.e->sum[RED] / p.e->n; |
122 |
< |
g = p.e->sum[GRN] / p.e->n; |
123 |
< |
b = p.e->sum[BLU] / p.e->n; |
121 |
> |
r = clrtab[h].sum[RED] / clrtab[h].n; |
122 |
> |
g = clrtab[h].sum[GRN] / clrtab[h].n; |
123 |
> |
b = clrtab[h].sum[BLU] / clrtab[h].n; |
124 |
|
/* check for movement */ |
125 |
< |
if (p.e->n == 1 || |
126 |
< |
(r-p.e->ent[RED])*(r-p.e->ent[RED]) + |
127 |
< |
(g-p.e->ent[GRN])*(g-p.e->ent[GRN]) + |
128 |
< |
(b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) { |
129 |
< |
p.e->ent[RED] = r; |
130 |
< |
p.e->ent[GRN] = g; /* reassign pixel */ |
131 |
< |
p.e->ent[BLU] = b; |
132 |
< |
#ifdef notdef |
133 |
< |
printf("pixel %d = (%d,%d,%d) (%d refs)\n", |
134 |
< |
h, r, g, b, p.e->n); |
125 |
> |
if (clrtab[h].n == 1 || |
126 |
> |
(r-clrtab[h].ent[RED])*(r-clrtab[h].ent[RED]) + |
127 |
> |
(g-clrtab[h].ent[GRN])*(g-clrtab[h].ent[GRN]) + |
128 |
> |
(b-clrtab[h].ent[BLU])*(b-clrtab[h].ent[BLU]) > MAXDST2) { |
129 |
> |
clrtab[h].ent[RED] = r; |
130 |
> |
clrtab[h].ent[GRN] = g; /* reassign pixel */ |
131 |
> |
clrtab[h].ent[BLU] = b; |
132 |
> |
#ifdef DEBUG |
133 |
> |
sprintf(errmsg, "pixel %d = (%d,%d,%d) (%d refs)\n", |
134 |
> |
h, r, g, b, clrtab[h].n); |
135 |
> |
eputs(errmsg); |
136 |
|
#endif |
137 |
|
(*set_pixel)(h, r, g, b); |
138 |
|
} |
156 |
|
set_cmap(rmap, gmap, bmap) /* set custom color correction map */ |
157 |
|
BYTE *rmap, *gmap, *bmap; |
158 |
|
{ |
159 |
< |
bcopy(rmap, clrmap[RED], 256); |
160 |
< |
bcopy(gmap, clrmap[GRN], 256); |
161 |
< |
bcopy(bmap, clrmap[BLU], 256); |
159 |
> |
bcopy((char *)rmap, (char *)clrmap[RED], 256); |
160 |
> |
bcopy((char *)gmap, (char *)clrmap[GRN], 256); |
161 |
> |
bcopy((char *)bmap, (char *)clrmap[BLU], 256); |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
+ |
map_color(rgb, col) /* map a color to a byte triplet */ |
166 |
+ |
BYTE rgb[3]; |
167 |
+ |
COLOR col; |
168 |
+ |
{ |
169 |
+ |
rgb[RED] = map_col(col,RED); |
170 |
+ |
rgb[GRN] = map_col(col,GRN); |
171 |
+ |
rgb[BLU] = map_col(col,BLU); |
172 |
+ |
} |
173 |
+ |
|
174 |
+ |
|
175 |
|
static |
176 |
|
cut(tree, level, box, c0, c1) /* partition color space */ |
177 |
|
register CNODE *tree; |
187 |
|
} |
188 |
|
/* split box */ |
189 |
|
*tree = split(box); |
190 |
< |
bcopy(box, kb, sizeof(kb)); |
190 |
> |
bcopy((char *)box, (char *)kb, sizeof(kb)); |
191 |
|
/* do left (lesser) branch */ |
192 |
|
kb[prim(*tree)][1] = part(*tree); |
193 |
|
cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1); |