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.2 by greg, Tue Oct 6 12:13:48 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1989 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16   *      distribution is correlated to the last.
17   */
18  
19 < #include "color.h"
19 > #include "standard.h"
20  
21 < #define NULL            0
21 > #include "color.h"
22                                  /* histogram resolution */
23   #define NRED            24
24   #define NGRN            32
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27                                  /* minimum box count for adaptive partition */
28   #define MINSAMP         7
29                                  /* maximum distance^2 before color reassign */
30 < #define MAXDST2         5
31 <                                /* maximum frame buffer depth */
32 < #define FBDEPTH         8
33 <                                /* color map resolution */
34 < #define MAPSIZ          256
30 > #define MAXDST2         12
31                                  /* map a color */
32 < #define map_col(c,p)    clrmap[ colval(c,p)<1. ? \
33 <                                (int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ]
32 > #define map_col(c,p)    clrmap[p][ colval(c,p)<1. ? \
33 >                                (int)(colval(c,p)*256.) : 255 ]
34                                  /* color partition tree */
35   #define CNODE           short
36 < #define set_branch(p,c) ((c)<<2|(p))
36 > #define set_branch(p,c) ((c)<<2|(p))
37   #define set_pval(pv)    ((pv)<<2|3)
38 + #define is_branch(cn)   (((cn)&3)!=3)
39   #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 < struct tabent {
44 > static struct tabent {
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];
46 >        int     n;              /* number of colors */
47 >        BYTE    ent[3];         /* current table value */
48 > }       *clrtab = NULL;
49 >                                /* color cube partition */
50 > static CNODE    *ctree = NULL;
51                                  /* our color correction map */
52 < static BYTE     clrmap[MAPSIZ];
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};
58                                /* color cube partition */
59 static CNODE    ctree[1<<(FBDEPTH+1)];
60                                /* callback for pixel assignment */
61 static int      (*set_pixel)();
57  
58  
59   int
60 < new_ctab(ncolors, cset)         /* start new color table with max ncolors */
60 > new_ctab(ncolors)               /* start new color table with max ncolors */
61   int     ncolors;
67 int     (*cset)();
62   {
63 <        if (ncolors < 1 || ncolors > 1<<FBDEPTH || cset == NULL)
63 >        int     treesize;
64 >
65 >        if (ncolors < 1)
66                  return(0);
67 <                                /* assign pixel callback routine */
68 <        set_pixel = cset;
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, FBDEPTH, CLRCUBE, 0, ncolors);
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   }
87  
88  
89   int
90 < get_pixel(col)                  /* get pixel for color */
90 > get_pixel(col, set_pixel)       /* get pixel for color */
91   COLOR   col;
92 + int     (*set_pixel)();
93   {
94 +        extern char     errmsg[];
95          int     r, g, b;
96          int     cv[3];
97 <        register union { CNODE *t; struct tabent *e; }  p;
97 >        register CNODE  *tp;
98          register int    h;
99                                                  /* map color */
100          r = map_col(col,RED);
# Line 100 | Line 107 | COLOR  col;
107                                                  /* add to histogram */
108          histo[cv[RED]][cv[GRN]][cv[BLU]]++;
109                                                  /* find pixel in tree */
110 <        p.t = ctree;
111 <        for (h = FBDEPTH; h > 0; h--) {
112 <                if (is_pval(*p.t))
106 <                        break;
107 <                if (cv[prim(*p.t)] < part(*p.t))
108 <                        p.t++;          /* left branch */
110 >        for (tp = ctree, h = 0; is_branch(*tp); h++)
111 >                if (cv[prim(*tp)] < part(*tp))
112 >                        tp += 1<<h;             /* left branch */
113                  else
114 <                        p.t += 1<<h;    /* right branch */
115 <        }
112 <        h = pval(*p.t);
114 >                        tp += 1<<(h+1);         /* right branch */
115 >        h = pval(*tp);
116                                                  /* add to color table */
117 <        p.e = clrtab + h;
118 <                                        /* add to sum */
119 <        p.e->sum[RED] += r;
120 <        p.e->sum[GRN] += g;
118 <        p.e->sum[BLU] += b;
119 <        p.e->n++;
117 >        clrtab[h].sum[RED] += r;
118 >        clrtab[h].sum[GRN] += g;
119 >        clrtab[h].sum[BLU] += b;
120 >        clrtab[h].n++;
121                                          /* recompute average */
122 <        r = p.e->sum[RED] / p.e->n;
123 <        g = p.e->sum[GRN] / p.e->n;
124 <        b = p.e->sum[BLU] / p.e->n;
122 >        r = clrtab[h].sum[RED] / clrtab[h].n;
123 >        g = clrtab[h].sum[GRN] / clrtab[h].n;
124 >        b = clrtab[h].sum[BLU] / clrtab[h].n;
125                                          /* check for movement */
126 <        if (p.e->n == 1 ||
127 <                        (r-p.e->ent[RED])*(r-p.e->ent[RED]) +
128 <                        (g-p.e->ent[GRN])*(g-p.e->ent[GRN]) +
129 <                        (b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) {
130 <                p.e->ent[RED] = r;
131 <                p.e->ent[GRN] = g;      /* reassign pixel */
132 <                p.e->ent[BLU] = b;
133 < #ifdef notdef
134 <                printf("pixel %d = (%d,%d,%d) (%d refs)\n",
135 <                                h, r, g, b, p.e->n);
126 >        if (clrtab[h].n == 1 ||
127 >                        (r-clrtab[h].ent[RED])*(r-clrtab[h].ent[RED]) +
128 >                        (g-clrtab[h].ent[GRN])*(g-clrtab[h].ent[GRN]) +
129 >                        (b-clrtab[h].ent[BLU])*(b-clrtab[h].ent[BLU]) > MAXDST2) {
130 >                clrtab[h].ent[RED] = r;
131 >                clrtab[h].ent[GRN] = g; /* reassign pixel */
132 >                clrtab[h].ent[BLU] = b;
133 > #ifdef DEBUG
134 >                sprintf(errmsg, "pixel %d = (%d,%d,%d) (%d refs)\n",
135 >                                h, r, g, b, clrtab[h].n);
136 >                eputs(errmsg);
137   #endif
138                  (*set_pixel)(h, r, g, b);
139          }
# Line 139 | Line 141 | COLOR  col;
141   }
142  
143  
144 < make_cmap(gam)                  /* make gamma correction map */
145 < double  gam;
144 > make_gmap(gam)                  /* make gamma correction map */
145 > double  gam;
146   {
145        extern double   pow();
147          register int    i;
148          
149 <        for (i = 0; i < MAPSIZ; i++)
150 <                clrmap[i] = 256.0 * pow(i/(double)MAPSIZ, 1.0/gam);
149 >        for (i = 0; i < 256; i++)
150 >                clrmap[RED][i] =
151 >                clrmap[GRN][i] =
152 >                clrmap[BLU][i] = 256.0 * pow((i+0.5)/256.0, 1.0/gam);
153   }
154  
155  
156 + set_cmap(rmap, gmap, bmap)      /* set custom color correction map */
157 + BYTE    *rmap, *gmap, *bmap;
158 + {
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, height, box, c0, c1)          /* partition color space */
176 > cut(tree, level, box, c0, c1)           /* partition color space */
177   register CNODE  *tree;
178 < int     height;
178 > int     level;
179   register int    box[3][2];
180   int     c0, c1;
181   {
# Line 165 | Line 187 | int    c0, c1;
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, height-1, kb, c0, (c0+c1)>>1);
193 >        cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1);
194                                                  /* do right branch */
195          kb[prim(*tree)][0] = part(*tree);
196          kb[prim(*tree)][1] = box[prim(*tree)][1];
197 <        cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1);
197 >        cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1);
198   }
199  
200  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines