ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/colortab.c
(Generate patch)

Comparing ray/src/rt/colortab.c (file contents):
Revision 1.2 by greg, Mon Oct 2 17:12:42 1989 UTC vs.
Revision 2.6 by greg, Tue Feb 25 02:47:22 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines