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 |
|
* |
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 |
> |
/* ==================================================================== |
19 |
> |
* The Radiance Software License, Version 1.0 |
20 |
> |
* |
21 |
> |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
22 |
> |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
23 |
> |
* |
24 |
> |
* Redistribution and use in source and binary forms, with or without |
25 |
> |
* modification, are permitted provided that the following conditions |
26 |
> |
* are met: |
27 |
> |
* |
28 |
> |
* 1. Redistributions of source code must retain the above copyright |
29 |
> |
* notice, this list of conditions and the following disclaimer. |
30 |
> |
* |
31 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
32 |
> |
* notice, this list of conditions and the following disclaimer in |
33 |
> |
* the documentation and/or other materials provided with the |
34 |
> |
* distribution. |
35 |
> |
* |
36 |
> |
* 3. The end-user documentation included with the redistribution, |
37 |
> |
* if any, must include the following acknowledgment: |
38 |
> |
* "This product includes Radiance software |
39 |
> |
* (http://radsite.lbl.gov/) |
40 |
> |
* developed by the Lawrence Berkeley National Laboratory |
41 |
> |
* (http://www.lbl.gov/)." |
42 |
> |
* Alternately, this acknowledgment may appear in the software itself, |
43 |
> |
* if and wherever such third-party acknowledgments normally appear. |
44 |
> |
* |
45 |
> |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
46 |
> |
* and "The Regents of the University of California" must |
47 |
> |
* not be used to endorse or promote products derived from this |
48 |
> |
* software without prior written permission. For written |
49 |
> |
* permission, please contact [email protected]. |
50 |
> |
* |
51 |
> |
* 5. Products derived from this software may not be called "Radiance", |
52 |
> |
* nor may "Radiance" appear in their name, without prior written |
53 |
> |
* permission of Lawrence Berkeley National Laboratory. |
54 |
> |
* |
55 |
> |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
56 |
> |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
57 |
> |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
58 |
> |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
59 |
> |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
60 |
> |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
61 |
> |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
62 |
> |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
63 |
> |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
64 |
> |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
65 |
> |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
66 |
> |
* SUCH DAMAGE. |
67 |
> |
* ==================================================================== |
68 |
> |
* |
69 |
> |
* This software consists of voluntary contributions made by many |
70 |
> |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
71 |
> |
* information on Lawrence Berkeley National Laboratory, please see |
72 |
> |
* <http://www.lbl.gov/>. |
73 |
> |
*/ |
74 |
|
|
75 |
< |
#define NULL 0 |
75 |
> |
#include "standard.h" |
76 |
> |
|
77 |
> |
#include "color.h" |
78 |
|
/* histogram resolution */ |
79 |
|
#define NRED 24 |
80 |
|
#define NGRN 32 |
89 |
|
(int)(colval(c,p)*256.) : 255 ] |
90 |
|
/* color partition tree */ |
91 |
|
#define CNODE short |
92 |
< |
#define set_branch(p,c) ((c)<<2|(p)) |
92 |
> |
#define set_branch(p,c) ((c)<<2|(p)) |
93 |
|
#define set_pval(pv) ((pv)<<2|3) |
94 |
|
#define is_branch(cn) (((cn)&3)!=3) |
95 |
|
#define is_pval(cn) (((cn)&3)==3) |
99 |
|
/* our color table */ |
100 |
|
static struct tabent { |
101 |
|
long sum[3]; /* sum of colors using this entry */ |
102 |
< |
long n; /* number of colors */ |
103 |
< |
short ent[3]; /* current table value */ |
102 |
> |
int n; /* number of colors */ |
103 |
> |
BYTE ent[3]; /* current table value */ |
104 |
|
} *clrtab = NULL; |
105 |
|
/* color cube partition */ |
106 |
|
static CNODE *ctree = NULL; |
109 |
|
/* histogram of colors used */ |
110 |
|
static unsigned short histo[NRED][NGRN][NBLU]; |
111 |
|
/* initial color cube boundary */ |
112 |
< |
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
112 |
> |
static int CLRCUBE[3][2] = {{0,NRED},{0,NGRN},{0,NBLU}}; |
113 |
|
|
114 |
+ |
static int split(); |
115 |
+ |
static void cut(); |
116 |
|
|
117 |
+ |
|
118 |
|
int |
119 |
|
new_ctab(ncolors) /* start new color table with max ncolors */ |
120 |
|
int ncolors; |
125 |
|
return(0); |
126 |
|
/* free old tables */ |
127 |
|
if (clrtab != NULL) |
128 |
< |
free((char *)clrtab); |
128 |
> |
free((void *)clrtab); |
129 |
|
if (ctree != NULL) |
130 |
< |
free((char *)ctree); |
130 |
> |
free((void *)ctree); |
131 |
|
/* get new tables */ |
132 |
|
for (treesize = 1; treesize < ncolors; treesize <<= 1) |
133 |
|
; |
148 |
|
int |
149 |
|
get_pixel(col, set_pixel) /* get pixel for color */ |
150 |
|
COLOR col; |
151 |
< |
int (*set_pixel)(); |
151 |
> |
void (*set_pixel)(); |
152 |
|
{ |
153 |
|
int r, g, b; |
154 |
|
int cv[3]; |
169 |
|
if (cv[prim(*tp)] < part(*tp)) |
170 |
|
tp += 1<<h; /* left branch */ |
171 |
|
else |
172 |
< |
tp += 1<<(h+1); /* right branch */ |
172 |
> |
tp += 1<<(h+1); /* right branch */ |
173 |
|
h = pval(*tp); |
174 |
|
/* add to color table */ |
175 |
|
clrtab[h].sum[RED] += r; |
186 |
|
(g-clrtab[h].ent[GRN])*(g-clrtab[h].ent[GRN]) + |
187 |
|
(b-clrtab[h].ent[BLU])*(b-clrtab[h].ent[BLU]) > MAXDST2) { |
188 |
|
clrtab[h].ent[RED] = r; |
189 |
< |
clrtab[h].ent[GRN] = g; /* reassign pixel */ |
189 |
> |
clrtab[h].ent[GRN] = g; /* reassign pixel */ |
190 |
|
clrtab[h].ent[BLU] = b; |
191 |
|
#ifdef DEBUG |
192 |
|
sprintf(errmsg, "pixel %d = (%d,%d,%d) (%d refs)\n", |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
+ |
void |
203 |
|
make_gmap(gam) /* make gamma correction map */ |
204 |
< |
double gam; |
204 |
> |
double gam; |
205 |
|
{ |
146 |
– |
extern double pow(); |
206 |
|
register int i; |
207 |
|
|
208 |
|
for (i = 0; i < 256; i++) |
209 |
|
clrmap[RED][i] = |
210 |
|
clrmap[GRN][i] = |
211 |
< |
clrmap[BLU][i] = 256.0 * pow(i/256.0, 1.0/gam); |
211 |
> |
clrmap[BLU][i] = 256.0 * pow((i+0.5)/256.0, 1.0/gam); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
+ |
void |
216 |
|
set_cmap(rmap, gmap, bmap) /* set custom color correction map */ |
217 |
|
BYTE *rmap, *gmap, *bmap; |
218 |
|
{ |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
< |
static |
225 |
> |
void |
226 |
> |
map_color(rgb, col) /* map a color to a byte triplet */ |
227 |
> |
BYTE rgb[3]; |
228 |
> |
COLOR col; |
229 |
> |
{ |
230 |
> |
rgb[RED] = map_col(col,RED); |
231 |
> |
rgb[GRN] = map_col(col,GRN); |
232 |
> |
rgb[BLU] = map_col(col,BLU); |
233 |
> |
} |
234 |
> |
|
235 |
> |
|
236 |
> |
static void |
237 |
|
cut(tree, level, box, c0, c1) /* partition color space */ |
238 |
|
register CNODE *tree; |
239 |
|
int level; |
266 |
|
#define c0 r |
267 |
|
register int r, g, b; |
268 |
|
int pri; |
269 |
< |
int t[HMAX], med; |
269 |
> |
long t[HMAX], med; |
270 |
|
/* find dominant axis */ |
271 |
|
pri = RED; |
272 |
|
if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0]) |