14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
– |
#ifdef SPEC_RGB |
18 |
– |
/* |
19 |
– |
* The following table contains the CIE tristimulus integrals |
20 |
– |
* for X, Y, and Z. The table is cumulative, so that |
21 |
– |
* each color coordinate integrates to 1. |
22 |
– |
*/ |
17 |
|
|
24 |
– |
#define STARTWL 380 /* starting wavelength (nanometers) */ |
25 |
– |
#define INCWL 10 /* wavelength increment */ |
26 |
– |
#define NINC 40 /* # of values */ |
27 |
– |
|
28 |
– |
static BYTE chroma[3][NINC] = { |
29 |
– |
{ /* X */ |
30 |
– |
0, 0, 0, 2, 6, 13, 22, 30, 36, 41, |
31 |
– |
42, 43, 43, 44, 46, 52, 60, 71, 87, 106, |
32 |
– |
128, 153, 178, 200, 219, 233, 243, 249, 252, 254, |
33 |
– |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
34 |
– |
}, { /* Y */ |
35 |
– |
0, 0, 0, 0, 0, 1, 2, 4, 7, 11, |
36 |
– |
17, 24, 34, 48, 64, 84, 105, 127, 148, 169, |
37 |
– |
188, 205, 220, 232, 240, 246, 250, 253, 254, 255, |
38 |
– |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
39 |
– |
}, { /* Z */ |
40 |
– |
0, 0, 2, 10, 32, 66, 118, 153, 191, 220, |
41 |
– |
237, 246, 251, 253, 254, 255, 255, 255, 255, 255, |
42 |
– |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
43 |
– |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
44 |
– |
} |
45 |
– |
}; |
46 |
– |
|
47 |
– |
|
48 |
– |
spec_rgb(col, s, e) /* comput RGB color from spectral range */ |
49 |
– |
COLOR col; |
50 |
– |
int s, e; |
51 |
– |
{ |
52 |
– |
COLOR ciecolor; |
53 |
– |
|
54 |
– |
spec_cie(ciecolor, s, e); |
55 |
– |
cie_rgb(col, ciecolor); |
56 |
– |
} |
57 |
– |
|
58 |
– |
|
59 |
– |
spec_cie(col, s, e) /* compute a color from a spectral range */ |
60 |
– |
COLOR col; /* returned color */ |
61 |
– |
int s, e; /* starting and ending wavelengths */ |
62 |
– |
{ |
63 |
– |
register int i, d, r; |
64 |
– |
|
65 |
– |
s -= STARTWL; |
66 |
– |
if (s < 0) |
67 |
– |
s = 0; |
68 |
– |
|
69 |
– |
e -= STARTWL; |
70 |
– |
if (e >= INCWL*(NINC - 1)) |
71 |
– |
e = INCWL*(NINC - 1) - 1; |
72 |
– |
|
73 |
– |
d = e / INCWL; /* interpolate values */ |
74 |
– |
r = e % INCWL; |
75 |
– |
for (i = 0; i < 3; i++) |
76 |
– |
col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r; |
77 |
– |
|
78 |
– |
d = s / INCWL; |
79 |
– |
r = s % INCWL; |
80 |
– |
for (i = 0; i < 3; i++) |
81 |
– |
col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r; |
82 |
– |
|
83 |
– |
col[RED] = (col[RED] + 0.5) / (256*INCWL); |
84 |
– |
col[GRN] = (col[GRN] + 0.5) / (256*INCWL); |
85 |
– |
col[BLU] = (col[BLU] + 0.5) / (256*INCWL); |
86 |
– |
} |
87 |
– |
|
88 |
– |
|
89 |
– |
cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ |
90 |
– |
register COLOR rgbcolor, ciecolor; |
91 |
– |
{ |
92 |
– |
static float cmat[3][3] = { |
93 |
– |
1.73, -.48, -.26, |
94 |
– |
-.81, 1.65, -.02, |
95 |
– |
.08, -.17, 1.28, |
96 |
– |
}; |
97 |
– |
register int i; |
98 |
– |
|
99 |
– |
for (i = 0; i < 3; i++) { |
100 |
– |
rgbcolor[i] = cmat[i][0]*ciecolor[0] + |
101 |
– |
cmat[i][1]*ciecolor[1] + |
102 |
– |
cmat[i][2]*ciecolor[2] ; |
103 |
– |
if (rgbcolor[i] < 0.0) |
104 |
– |
rgbcolor[i] = 0.0; |
105 |
– |
} |
106 |
– |
} |
107 |
– |
#endif |
108 |
– |
|
109 |
– |
|
110 |
– |
fputresolu(ord, xres, yres, fp) /* put x and y resolution */ |
111 |
– |
register int ord; |
112 |
– |
int xres, yres; |
113 |
– |
FILE *fp; |
114 |
– |
{ |
115 |
– |
if (ord&YMAJOR) |
116 |
– |
fprintf(fp, "%cY %d %cX %d\n", |
117 |
– |
ord&YDECR ? '-' : '+', yres, |
118 |
– |
ord&XDECR ? '-' : '+', xres); |
119 |
– |
else |
120 |
– |
fprintf(fp, "%cX %d %cY %d\n", |
121 |
– |
ord&XDECR ? '-' : '+', xres, |
122 |
– |
ord&YDECR ? '-' : '+', yres); |
123 |
– |
} |
124 |
– |
|
125 |
– |
|
126 |
– |
fgetresolu(xrp, yrp, fp) /* get x and y resolution */ |
127 |
– |
int *xrp, *yrp; |
128 |
– |
FILE *fp; |
129 |
– |
{ |
130 |
– |
char buf[64], *xndx, *yndx; |
131 |
– |
register char *cp; |
132 |
– |
register int ord; |
133 |
– |
|
134 |
– |
if (fgets(buf, sizeof(buf), fp) == NULL) |
135 |
– |
return(-1); |
136 |
– |
xndx = yndx = NULL; |
137 |
– |
for (cp = buf+1; *cp; cp++) |
138 |
– |
if (*cp == 'X') |
139 |
– |
xndx = cp; |
140 |
– |
else if (*cp == 'Y') |
141 |
– |
yndx = cp; |
142 |
– |
if (xndx == NULL || yndx == NULL) |
143 |
– |
return(-1); |
144 |
– |
ord = 0; |
145 |
– |
if (xndx > yndx) ord |= YMAJOR; |
146 |
– |
if (xndx[-1] == '-') ord |= XDECR; |
147 |
– |
if (yndx[-1] == '-') ord |= YDECR; |
148 |
– |
if ((*xrp = atoi(xndx+1)) <= 0) |
149 |
– |
return(-1); |
150 |
– |
if ((*yrp = atoi(yndx+1)) <= 0) |
151 |
– |
return(-1); |
152 |
– |
return(ord); |
153 |
– |
} |
154 |
– |
|
155 |
– |
|
18 |
|
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
19 |
|
register COLR *scanline; |
20 |
|
int len; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
< |
int |
233 |
< |
colr_norm(clr, nclr) /* normalize a short color, return shift */ |
234 |
< |
COLR clr, nclr; |
232 |
> |
normcolrs(scan, len, adjust) /* normalize a scanline of colrs */ |
233 |
> |
register COLR *scan; |
234 |
> |
int len; |
235 |
> |
int adjust; |
236 |
|
{ |
237 |
|
register int c; |
238 |
< |
register int shift = clr[EXP]-COLXS; |
238 |
> |
register int shift; |
239 |
|
|
240 |
< |
if (shift > 0) { |
241 |
< |
if (shift >= 8) { |
242 |
< |
nclr[RED] = nclr[GRN] = nclr[BLU] = 255; |
243 |
< |
} else { |
244 |
< |
c = clr[RED] << shift; |
245 |
< |
nclr[RED] = c > 255 ? 255 : c; |
246 |
< |
c = clr[GRN] << shift; |
247 |
< |
nclr[GRN] = c > 255 ? 255 : c; |
248 |
< |
c = clr[BLU] << shift; |
249 |
< |
nclr[BLU] = c > 255 ? 255 : c; |
240 |
> |
while (len-- > 0) { |
241 |
> |
shift = scan[0][EXP] + adjust - COLXS; |
242 |
> |
if (shift > 0) { |
243 |
> |
if (shift > 8) { |
244 |
> |
scan[0][RED] = |
245 |
> |
scan[0][GRN] = |
246 |
> |
scan[0][BLU] = 255; |
247 |
> |
} else { |
248 |
> |
shift--; |
249 |
> |
c = (scan[0][RED]<<1 | 1) << shift; |
250 |
> |
scan[0][RED] = c > 255 ? 255 : c; |
251 |
> |
c = (scan[0][GRN]<<1 | 1) << shift; |
252 |
> |
scan[0][GRN] = c > 255 ? 255 : c; |
253 |
> |
c = (scan[0][BLU]<<1 | 1) << shift; |
254 |
> |
scan[0][BLU] = c > 255 ? 255 : c; |
255 |
> |
} |
256 |
> |
} else if (shift < 0) { |
257 |
> |
if (shift < -8) { |
258 |
> |
scan[0][RED] = |
259 |
> |
scan[0][GRN] = |
260 |
> |
scan[0][BLU] = 0; |
261 |
> |
} else { |
262 |
> |
shift = -1-shift; |
263 |
> |
scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1; |
264 |
> |
scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1; |
265 |
> |
scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1; |
266 |
> |
} |
267 |
|
} |
268 |
< |
} else if (shift < 0) { |
269 |
< |
if (shift <= -8) { |
390 |
< |
nclr[RED] = nclr[GRN] = nclr[BLU] = 0; |
391 |
< |
} else { |
392 |
< |
nclr[RED] = clr[RED] >> -shift; |
393 |
< |
nclr[GRN] = clr[GRN] >> -shift; |
394 |
< |
nclr[BLU] = clr[BLU] >> -shift; |
395 |
< |
} |
396 |
< |
} else { |
397 |
< |
nclr[RED] = clr[RED]; |
398 |
< |
nclr[GRN] = clr[GRN]; |
399 |
< |
nclr[BLU] = clr[BLU]; |
268 |
> |
scan[0][EXP] = COLXS - adjust; |
269 |
> |
scan++; |
270 |
|
} |
271 |
< |
nclr[EXP] = COLXS; |
272 |
< |
return(shift); |
271 |
> |
} |
272 |
> |
|
273 |
> |
|
274 |
> |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
275 |
> |
register COLOR c1, c2; |
276 |
> |
double md; |
277 |
> |
{ |
278 |
> |
register int i; |
279 |
> |
|
280 |
> |
for (i = 0; i < 3; i++) |
281 |
> |
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || |
282 |
> |
colval(c2,i)-colval(c1,i) > md*colval(c1,i)) |
283 |
> |
return(1); |
284 |
> |
return(0); |
285 |
|
} |