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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines