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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines