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 1.7 by greg, Thu Oct 12 11:12:48 1989 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   #define NULL            0
22                                  /* histogram resolution */
23 < #define NRED            28
23 > #define NRED            24
24   #define NGRN            32
25 < #define NBLU            22
25 > #define NBLU            16
26   #define HMAX            NGRN
27                                  /* minimum box count for adaptive partition */
28   #define MINSAMP         7
29 +                                /* maximum distance^2 before color reassign */
30 + #define MAXDST2         12
31                                  /* maximum frame buffer depth */
32   #define FBDEPTH         8
31                                /* color map resolution */
32 #define MAPSIZ          128
33                                  /* map a color */
34   #define map_col(c,p)    clrmap[p][ colval(c,p)<1. ? \
35 <                                (int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ]
35 >                                (int)(colval(c,p)*256.) : 255 ]
36                                  /* color partition tree */
37   #define CNODE           short
38   #define set_branch(p,c) ((c)<<2|(p))
39   #define set_pval(pv)    ((pv)<<2|3)
40 + #define is_branch(cn)   (((cn)&3)!=3)
41   #define is_pval(cn)     (((cn)&3)==3)
42   #define part(cn)        ((cn)>>2)
43   #define prim(cn)        ((cn)&3)
44   #define pval(cn)        ((cn)>>2)
45                                  /* our color table */
46 < COLR    clrtab[1<<FBDEPTH];
46 > static struct tabent {
47 >        long    sum[3];         /* sum of colors using this entry */
48 >        long    n;              /* number of colors */
49 >        short   ent[3];         /* current table value */
50 > }       clrtab[1<<FBDEPTH];
51                                  /* our color correction map */
52 < static BYTE     clrmap[3][MAPSIZ];
52 > static BYTE     clrmap[3][256];
53                                  /* histogram of colors used */
54   static unsigned histo[NRED][NGRN][NBLU];
55                                  /* initial color cube boundaries */
# Line 53 | Line 58 | static int     CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU};
58   static CNODE    ctree[1<<(FBDEPTH+1)];
59  
60  
61 < COLR *
62 < get_ctab(ncolors)       /* assign a color table with max ncolors */
61 > int
62 > new_ctab(ncolors)               /* start new color table with max ncolors */
63   int     ncolors;
64   {
65 <        if (ncolors < 1 || ncolors > 1<<FBDEPTH)
66 <                return(NULL);
67 <                                /* partition color table */
68 <        cut(ctree, FBDEPTH, CLRCUBE, 0, ncolors);
65 >        if (ncolors < 1)
66 >                return(0);
67 >        if (ncolors > 1<<FBDEPTH)
68 >                ncolors = 1<<FBDEPTH;
69 >                                /* clear color table */
70 >        bzero(clrtab, sizeof(clrtab));
71 >                                /* partition color space */
72 >        cut(ctree, 0, CLRCUBE, 0, ncolors);
73                                  /* clear histogram */
74          bzero(histo, sizeof(histo));
75 <                                /* return color table */
76 <        return(clrtab);
75 >                                /* return number of colors used */
76 >        return(ncolors);
77   }
78  
79  
80   int
81 < get_pixel(col)                  /* get pixel for color */
81 > get_pixel(col, set_pixel)       /* get pixel for color */
82   COLOR   col;
83 + int     (*set_pixel)();
84   {
85 +        int     r, g, b;
86          int     cv[3];
87 <        register CNODE  *tp;
87 >        register union { CNODE *t; struct tabent *e; }  p;
88          register int    h;
89 <                                        /* map color */
90 <        cv[RED] = map_col(col,RED);
91 <        cv[GRN] = map_col(col,GRN);
92 <        cv[BLU] = map_col(col,BLU);
93 <                                        /* add to histogram */
89 >                                                /* map color */
90 >        r = map_col(col,RED);
91 >        g = map_col(col,GRN);
92 >        b = map_col(col,BLU);
93 >                                                /* reduce resolution */
94 >        cv[RED] = (r*NRED)>>8;
95 >        cv[GRN] = (g*NGRN)>>8;
96 >        cv[BLU] = (b*NBLU)>>8;
97 >                                                /* add to histogram */
98          histo[cv[RED]][cv[GRN]][cv[BLU]]++;
99 <                                        /* find pixel value in tree */
100 <        tp = ctree;
101 <        for (h = FBDEPTH; h > 0; h--) {
102 <                if (is_pval(*tp))
88 <                        break;
89 <                if (cv[prim(*tp)] < part(*tp))
90 <                        tp++;                   /* left branch */
99 >                                                /* find pixel in tree */
100 >        for (p.t = ctree, h = 0; is_branch(*p.t); h++)
101 >                if (cv[prim(*p.t)] < part(*p.t))
102 >                        p.t += 1<<h;            /* left branch */
103                  else
104 <                        tp += 1<<h;             /* right branch */
105 <        }
104 >                        p.t += 1<<(h+1);        /* right branch */
105 >        h = pval(*p.t);
106 >                                                /* add to color table */
107 >        p.e = clrtab + h;
108 >                                        /* add to sum */
109 >        p.e->sum[RED] += r;
110 >        p.e->sum[GRN] += g;
111 >        p.e->sum[BLU] += b;
112 >        p.e->n++;
113 >                                        /* recompute average */
114 >        r = p.e->sum[RED] / p.e->n;
115 >        g = p.e->sum[GRN] / p.e->n;
116 >        b = p.e->sum[BLU] / p.e->n;
117 >                                        /* check for movement */
118 >        if (p.e->n == 1 ||
119 >                        (r-p.e->ent[RED])*(r-p.e->ent[RED]) +
120 >                        (g-p.e->ent[GRN])*(g-p.e->ent[GRN]) +
121 >                        (b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) {
122 >                p.e->ent[RED] = r;
123 >                p.e->ent[GRN] = g;      /* reassign pixel */
124 >                p.e->ent[BLU] = b;
125   #ifdef notdef
126 <        printf("distance (%d,%d,%d)\n",
127 <                cv[RED] - clrtab[pval(*tp)][RED]*NRED/256,
97 <                cv[GRN] - clrtab[pval(*tp)][GRN]*NGRN/256,
98 <                cv[BLU] - clrtab[pval(*tp)][BLU]*NBLU/256);
126 >                printf("pixel %d = (%d,%d,%d) (%d refs)\n",
127 >                                h, r, g, b, p.e->n);
128   #endif
129 <        return(pval(*tp));
129 >                (*set_pixel)(h, r, g, b);
130 >        }
131 >        return(h);                              /* return pixel value */
132   }
133  
134  
135 < make_cmap(gam)                  /* make gamma correction map */
135 > make_gmap(gam)                  /* make gamma correction map */
136   double  gam;
137   {
138          extern double   pow();
108        double  d;
139          register int    i;
140          
141 <        for (i = 0; i < MAPSIZ; i++) {
142 <                d = pow(i/(double)MAPSIZ, 1.0/gam);
143 <                clrmap[RED][i] = d * NRED;
144 <                clrmap[GRN][i] = d * NGRN;
115 <                clrmap[BLU][i] = d * NBLU;
116 <        }
141 >        for (i = 0; i < 256; i++)
142 >                clrmap[RED][i] =
143 >                clrmap[GRN][i] =
144 >                clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam);
145   }
146  
147  
148 + set_cmap(rmap, gmap, bmap)      /* set custom color correction map */
149 + BYTE    *rmap, *gmap, *bmap;
150 + {
151 +        bcopy(rmap, clrmap[RED], 256);
152 +        bcopy(gmap, clrmap[GRN], 256);
153 +        bcopy(bmap, clrmap[BLU], 256);
154 + }
155 +
156 +
157   static
158 < cut(tree, height, box, c0, c1)          /* partition color space */
158 > cut(tree, level, box, c0, c1)           /* partition color space */
159   register CNODE  *tree;
160 < int     height;
160 > int     level;
161   register int    box[3][2];
162   int     c0, c1;
163   {
164          int     kb[3][2];
165          
166 <        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;
166 >        if (c1-c0 <= 1) {               /* assign pixel */
167                  *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
168                  return;
169          }
170                                          /* split box */
171          *tree = split(box);
172          bcopy(box, kb, sizeof(kb));
173 +                                                /* do left (lesser) branch */
174          kb[prim(*tree)][1] = part(*tree);
175 <        cut(tree+1, height-1, kb, c0, (c0+c1)>>1);              /* lesser */
175 >        cut(tree+(1<<level), level+1, kb, c0, (c0+c1)>>1);
176 >                                                /* do right branch */
177          kb[prim(*tree)][0] = part(*tree);
178          kb[prim(*tree)][1] = box[prim(*tree)][1];
179 <        cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1);    /* greater */
179 >        cut(tree+(1<<(level+1)), level+1, kb, (c0+c1)>>1, c1);
180   }
181  
182  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines