ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/colortab.c
Revision: 1.10
Committed: Tue Jan 16 16:00:00 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +21 -23 lines
Log Message:
got rid of pointer nonsense that was confusing optimizer

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1989 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * colortab.c - allocate and control dynamic color table.
9     *
10     * We start off with a uniform partition of color space.
11     * As pixels are sent to the frame buffer, a histogram is built.
12     * When a new color table is requested, the histogram is used
13     * to make a pseudo-optimal partition, after which the
14     * histogram is cleared. This algorithm
15     * performs only as well as the next drawing's color
16     * distribution is correlated to the last.
17     */
18    
19     #include "color.h"
20    
21     #define NULL 0
22     /* histogram resolution */
23 greg 1.3 #define NRED 24
24 greg 1.1 #define NGRN 32
25 greg 1.3 #define NBLU 16
26 greg 1.1 #define HMAX NGRN
27     /* minimum box count for adaptive partition */
28     #define MINSAMP 7
29 greg 1.3 /* maximum distance^2 before color reassign */
30 greg 1.5 #define MAXDST2 12
31 greg 1.1 /* map a color */
32 greg 1.4 #define map_col(c,p) clrmap[p][ colval(c,p)<1. ? \
33     (int)(colval(c,p)*256.) : 255 ]
34 greg 1.1 /* 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 greg 1.7 #define is_branch(cn) (((cn)&3)!=3)
39 greg 1.1 #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 greg 1.6 static struct tabent {
45 greg 1.3 long sum[3]; /* sum of colors using this entry */
46     long n; /* number of colors */
47     short ent[3]; /* current table value */
48 greg 1.8 } *clrtab = NULL;
49     /* color cube partition */
50     static CNODE *ctree = NULL;
51 greg 1.1 /* our color correction map */
52 greg 1.4 static BYTE clrmap[3][256];
53 greg 1.1 /* histogram of colors used */
54 greg 1.9 static unsigned short histo[NRED][NGRN][NBLU];
55 greg 1.8 /* initial color cube boundary */
56 greg 1.1 static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU};
57    
58    
59 greg 1.3 int
60 greg 1.4 new_ctab(ncolors) /* start new color table with max ncolors */
61 greg 1.1 int ncolors;
62     {
63 greg 1.8 int treesize;
64    
65 greg 1.6 if (ncolors < 1)
66 greg 1.3 return(0);
67 greg 1.8 /* 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 greg 1.3 /* partition color space */
81 greg 1.7 cut(ctree, 0, CLRCUBE, 0, ncolors);
82 greg 1.1 /* clear histogram */
83     bzero(histo, sizeof(histo));
84 greg 1.3 /* return number of colors used */
85     return(ncolors);
86 greg 1.1 }
87    
88    
89     int
90 greg 1.4 get_pixel(col, set_pixel) /* get pixel for color */
91 greg 1.1 COLOR col;
92 greg 1.4 int (*set_pixel)();
93 greg 1.1 {
94 greg 1.3 int r, g, b;
95 greg 1.1 int cv[3];
96 greg 1.10 register CNODE *tp;
97 greg 1.1 register int h;
98 greg 1.3 /* map color */
99     r = map_col(col,RED);
100     g = map_col(col,GRN);
101     b = map_col(col,BLU);
102     /* reduce resolution */
103     cv[RED] = (r*NRED)>>8;
104     cv[GRN] = (g*NGRN)>>8;
105     cv[BLU] = (b*NBLU)>>8;
106     /* add to histogram */
107 greg 1.1 histo[cv[RED]][cv[GRN]][cv[BLU]]++;
108 greg 1.3 /* find pixel in tree */
109 greg 1.10 for (tp = ctree, h = 0; is_branch(*tp); h++)
110     if (cv[prim(*tp)] < part(*tp))
111     tp += 1<<h; /* left branch */
112 greg 1.1 else
113 greg 1.10 tp += 1<<(h+1); /* right branch */
114     h = pval(*tp);
115 greg 1.3 /* add to color table */
116 greg 1.10 clrtab[h].sum[RED] += r;
117     clrtab[h].sum[GRN] += g;
118     clrtab[h].sum[BLU] += b;
119     clrtab[h].n++;
120 greg 1.3 /* recompute average */
121 greg 1.10 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 greg 1.3 /* check for movement */
125 greg 1.10 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 greg 1.1 #ifdef notdef
133 greg 1.3 printf("pixel %d = (%d,%d,%d) (%d refs)\n",
134 greg 1.10 h, r, g, b, clrtab[h].n);
135 greg 1.1 #endif
136 greg 1.3 (*set_pixel)(h, r, g, b);
137     }
138     return(h); /* return pixel value */
139 greg 1.1 }
140    
141    
142 greg 1.4 make_gmap(gam) /* make gamma correction map */
143 greg 1.1 double gam;
144     {
145     extern double pow();
146     register int i;
147    
148 greg 1.4 for (i = 0; i < 256; i++)
149     clrmap[RED][i] =
150     clrmap[GRN][i] =
151     clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam);
152     }
153    
154    
155     set_cmap(rmap, gmap, bmap) /* set custom color correction map */
156     BYTE *rmap, *gmap, *bmap;
157     {
158     bcopy(rmap, clrmap[RED], 256);
159     bcopy(gmap, clrmap[GRN], 256);
160     bcopy(bmap, clrmap[BLU], 256);
161 greg 1.1 }
162    
163    
164     static
165 greg 1.7 cut(tree, level, box, c0, c1) /* partition color space */
166 greg 1.1 register CNODE *tree;
167 greg 1.7 int level;
168 greg 1.1 register int box[3][2];
169     int c0, c1;
170     {
171     int kb[3][2];
172    
173 greg 1.3 if (c1-c0 <= 1) { /* assign pixel */
174 greg 1.1 *tree = set_pval(c0);
175     return;
176     }
177     /* split box */
178     *tree = split(box);
179     bcopy(box, kb, sizeof(kb));
180 greg 1.3 /* do left (lesser) branch */
181 greg 1.1 kb[prim(*tree)][1] = part(*tree);
182 greg 1.7 cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1);
183 greg 1.3 /* do right branch */
184 greg 1.1 kb[prim(*tree)][0] = part(*tree);
185     kb[prim(*tree)][1] = box[prim(*tree)][1];
186 greg 1.7 cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1);
187 greg 1.1 }
188    
189    
190     static int
191     split(box) /* find median cut for box */
192     register int box[3][2];
193     {
194     #define c0 r
195     register int r, g, b;
196     int pri;
197     int t[HMAX], med;
198     /* find dominant axis */
199     pri = RED;
200     if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0])
201     pri = GRN;
202     if (box[BLU][1]-box[BLU][0] > box[pri][1]-box[pri][0])
203     pri = BLU;
204     /* sum histogram over box */
205     med = 0;
206     switch (pri) {
207     case RED:
208     for (r = box[RED][0]; r < box[RED][1]; r++) {
209     t[r] = 0;
210     for (g = box[GRN][0]; g < box[GRN][1]; g++)
211     for (b = box[BLU][0]; b < box[BLU][1]; b++)
212     t[r] += histo[r][g][b];
213     med += t[r];
214     }
215     break;
216     case GRN:
217     for (g = box[GRN][0]; g < box[GRN][1]; g++) {
218     t[g] = 0;
219     for (b = box[BLU][0]; b < box[BLU][1]; b++)
220     for (r = box[RED][0]; r < box[RED][1]; r++)
221     t[g] += histo[r][g][b];
222     med += t[g];
223     }
224     break;
225     case BLU:
226     for (b = box[BLU][0]; b < box[BLU][1]; b++) {
227     t[b] = 0;
228     for (r = box[RED][0]; r < box[RED][1]; r++)
229     for (g = box[GRN][0]; g < box[GRN][1]; g++)
230     t[b] += histo[r][g][b];
231     med += t[b];
232     }
233     break;
234     }
235     if (med < MINSAMP) /* if too sparse, split at midpoint */
236     return(set_branch(pri,(box[pri][0]+box[pri][1])>>1));
237     /* find median position */
238     med >>= 1;
239     for (c0 = box[pri][0]; med > 0; c0++)
240     med -= t[c0];
241     if (c0 > (box[pri][0]+box[pri][1])>>1) /* if past the midpoint */
242     c0--; /* part left of median */
243     return(set_branch(pri,c0));
244     #undef c0
245     }