1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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 |
> |
#define MINELEN 8 /* minimum scanline length for encoding */ |
18 |
> |
#define MAXELEN 0x7ffe /* maximum scanline length for encoding */ |
19 |
> |
#define MINRUN 4 /* minimum run length */ |
20 |
|
|
21 |
< |
#define STARTWL 380 /* starting wavelength (nanometers) */ |
22 |
< |
#define INCWL 10 /* wavelength increment */ |
23 |
< |
#define NINC 40 /* # of values */ |
21 |
> |
#ifndef frexp |
22 |
> |
extern double frexp(); |
23 |
> |
#endif |
24 |
|
|
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 |
– |
}; |
25 |
|
|
26 |
< |
|
27 |
< |
spec_rgb(col, s, e) /* comput RGB color from spectral range */ |
28 |
< |
COLOR col; |
50 |
< |
int s, e; |
26 |
> |
char * |
27 |
> |
tempbuffer(len) /* get a temporary buffer */ |
28 |
> |
unsigned len; |
29 |
|
{ |
30 |
< |
COLOR ciecolor; |
30 |
> |
extern char *malloc(), *realloc(); |
31 |
> |
static char *tempbuf = NULL; |
32 |
> |
static unsigned tempbuflen = 0; |
33 |
|
|
34 |
< |
spec_cie(ciecolor, s, e); |
35 |
< |
cie_rgb(col, ciecolor); |
34 |
> |
if (len > tempbuflen) { |
35 |
> |
if (tempbuflen > 0) |
36 |
> |
tempbuf = realloc(tempbuf, len); |
37 |
> |
else |
38 |
> |
tempbuf = malloc(len); |
39 |
> |
tempbuflen = tempbuf==NULL ? 0 : len; |
40 |
> |
} |
41 |
> |
return(tempbuf); |
42 |
|
} |
43 |
|
|
44 |
|
|
45 |
< |
spec_cie(col, s, e) /* compute a color from a spectral range */ |
46 |
< |
COLOR col; /* returned color */ |
47 |
< |
int s, e; /* starting and ending wavelengths */ |
45 |
> |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
46 |
> |
register COLR *scanline; |
47 |
> |
unsigned len; |
48 |
> |
register FILE *fp; |
49 |
|
{ |
50 |
< |
register int i, d, r; |
50 |
> |
register int i, j, beg, cnt; |
51 |
> |
int c2; |
52 |
|
|
53 |
< |
s -= STARTWL; |
54 |
< |
if (s < 0) |
55 |
< |
s = 0; |
56 |
< |
|
57 |
< |
e -= STARTWL; |
58 |
< |
if (e >= INCWL*(NINC - 1)) |
59 |
< |
e = INCWL*(NINC - 1) - 1; |
60 |
< |
|
61 |
< |
d = e / INCWL; /* interpolate values */ |
62 |
< |
r = e % INCWL; |
63 |
< |
for (i = 0; i < 3; i++) |
64 |
< |
col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r; |
65 |
< |
|
66 |
< |
d = s / INCWL; |
67 |
< |
r = s % INCWL; |
68 |
< |
for (i = 0; i < 3; i++) |
69 |
< |
col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r; |
70 |
< |
|
71 |
< |
col[RED] = (col[RED] + 0.5) / (256*INCWL); |
72 |
< |
col[GRN] = (col[GRN] + 0.5) / (256*INCWL); |
73 |
< |
col[BLU] = (col[BLU] + 0.5) / (256*INCWL); |
74 |
< |
} |
75 |
< |
|
76 |
< |
|
77 |
< |
cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ |
78 |
< |
register COLOR rgbcolor, ciecolor; |
79 |
< |
{ |
80 |
< |
static float cmat[3][3] = { |
81 |
< |
1.73, -.48, -.26, |
82 |
< |
-.81, 1.65, -.02, |
83 |
< |
.08, -.17, 1.28, |
84 |
< |
}; |
85 |
< |
register int i; |
86 |
< |
|
87 |
< |
for (i = 0; i < 3; i++) { |
88 |
< |
rgbcolor[i] = cmat[i][0]*ciecolor[0] + |
89 |
< |
cmat[i][1]*ciecolor[1] + |
90 |
< |
cmat[i][2]*ciecolor[2] ; |
91 |
< |
if (rgbcolor[i] < 0.0) |
104 |
< |
rgbcolor[i] = 0.0; |
53 |
> |
if (len < MINELEN | len > MAXELEN) /* OOBs, write out flat */ |
54 |
> |
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
55 |
> |
/* put magic header */ |
56 |
> |
putc(2, fp); |
57 |
> |
putc(2, fp); |
58 |
> |
putc(len>>8, fp); |
59 |
> |
putc(len&255, fp); |
60 |
> |
/* put components seperately */ |
61 |
> |
for (i = 0; i < 4; i++) { |
62 |
> |
for (j = 0; j < len; j += cnt) { /* find next run */ |
63 |
> |
for (beg = j; beg < len; beg += cnt) { |
64 |
> |
for (cnt = 1; cnt < 127 && beg+cnt < len && |
65 |
> |
scanline[beg+cnt][i] == scanline[beg][i]; cnt++) |
66 |
> |
; |
67 |
> |
if (cnt >= MINRUN) |
68 |
> |
break; /* long enough */ |
69 |
> |
} |
70 |
> |
if (beg-j > 1 && beg-j < MINRUN) { |
71 |
> |
c2 = j+1; |
72 |
> |
while (scanline[c2++][i] == scanline[j][i]) |
73 |
> |
if (c2 == beg) { /* short run */ |
74 |
> |
putc(128+beg-j, fp); |
75 |
> |
putc(scanline[j][i], fp); |
76 |
> |
j = beg; |
77 |
> |
break; |
78 |
> |
} |
79 |
> |
} |
80 |
> |
while (j < beg) { /* write out non-run */ |
81 |
> |
if ((c2 = beg-j) > 128) c2 = 128; |
82 |
> |
putc(c2, fp); |
83 |
> |
while (c2--) |
84 |
> |
putc(scanline[j++][i], fp); |
85 |
> |
} |
86 |
> |
if (cnt >= MINRUN) { /* write out run */ |
87 |
> |
putc(128+cnt, fp); |
88 |
> |
putc(scanline[beg][i], fp); |
89 |
> |
} else |
90 |
> |
cnt = 0; |
91 |
> |
} |
92 |
|
} |
93 |
+ |
return(ferror(fp) ? -1 : 0); |
94 |
|
} |
107 |
– |
#endif |
95 |
|
|
96 |
|
|
97 |
< |
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 |
< |
|
156 |
< |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
97 |
> |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
98 |
|
register COLR *scanline; |
99 |
|
int len; |
100 |
|
register FILE *fp; |
101 |
|
{ |
102 |
< |
COLR lastcolr; |
103 |
< |
int rept; |
104 |
< |
|
105 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
106 |
< |
lastcolr[EXP] = 0; |
107 |
< |
rept = 0; |
108 |
< |
|
109 |
< |
while (len > 0) { |
110 |
< |
if (scanline[0][EXP] == lastcolr[EXP] && |
111 |
< |
scanline[0][RED] == lastcolr[RED] && |
171 |
< |
scanline[0][GRN] == lastcolr[GRN] && |
172 |
< |
scanline[0][BLU] == lastcolr[BLU]) |
173 |
< |
rept++; |
174 |
< |
else { |
175 |
< |
while (rept) { /* write out count */ |
176 |
< |
putc(1, fp); |
177 |
< |
putc(1, fp); |
178 |
< |
putc(1, fp); |
179 |
< |
putc(rept & 255, fp); |
180 |
< |
rept >>= 8; |
181 |
< |
} |
182 |
< |
putc(scanline[0][RED], fp); /* new color */ |
183 |
< |
putc(scanline[0][GRN], fp); |
184 |
< |
putc(scanline[0][BLU], fp); |
185 |
< |
putc(scanline[0][EXP], fp); |
186 |
< |
copycolr(lastcolr, scanline[0]); |
187 |
< |
rept = 0; |
188 |
< |
} |
189 |
< |
scanline++; |
190 |
< |
len--; |
102 |
> |
register int i, j; |
103 |
> |
int code; |
104 |
> |
/* determine scanline type */ |
105 |
> |
if (len < MINELEN | len > MAXELEN) |
106 |
> |
return(oldreadcolrs(scanline, len, fp)); |
107 |
> |
if ((i = getc(fp)) == EOF) |
108 |
> |
return(-1); |
109 |
> |
if (i != 2) { |
110 |
> |
ungetc(i, fp); |
111 |
> |
return(oldreadcolrs(scanline, len, fp)); |
112 |
|
} |
113 |
< |
while (rept) { /* write out count */ |
114 |
< |
putc(1, fp); |
115 |
< |
putc(1, fp); |
116 |
< |
putc(1, fp); |
117 |
< |
putc(rept & 255, fp); |
118 |
< |
rept >>= 8; |
113 |
> |
scanline[0][GRN] = getc(fp); |
114 |
> |
scanline[0][BLU] = getc(fp); |
115 |
> |
if ((i = getc(fp)) == EOF) |
116 |
> |
return(-1); |
117 |
> |
if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) { |
118 |
> |
scanline[0][RED] = 2; |
119 |
> |
scanline[0][EXP] = i; |
120 |
> |
return(oldreadcolrs(scanline+1, len-1, fp)); |
121 |
|
} |
122 |
< |
return(ferror(fp) ? -1 : 0); |
122 |
> |
if ((scanline[0][BLU]<<8 | i) != len) |
123 |
> |
return(-1); /* length mismatch! */ |
124 |
> |
/* read each component */ |
125 |
> |
for (i = 0; i < 4; i++) |
126 |
> |
for (j = 0; j < len; ) { |
127 |
> |
if ((code = getc(fp)) == EOF) |
128 |
> |
return(-1); |
129 |
> |
if (code > 128) { /* run */ |
130 |
> |
scanline[j++][i] = getc(fp); |
131 |
> |
for (code &= 127; --code; j++) |
132 |
> |
scanline[j][i] = scanline[j-1][i]; |
133 |
> |
} else /* non-run */ |
134 |
> |
while (code--) |
135 |
> |
scanline[j++][i] = getc(fp); |
136 |
> |
} |
137 |
> |
return(feof(fp) ? -1 : 0); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
< |
freadcolrs(scanline, len, fp) /* read in a colr scanline */ |
141 |
> |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
142 |
|
register COLR *scanline; |
143 |
|
int len; |
144 |
|
register FILE *fp; |
177 |
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
178 |
|
register COLOR *scanline; |
179 |
|
int len; |
180 |
< |
register FILE *fp; |
180 |
> |
FILE *fp; |
181 |
|
{ |
182 |
< |
COLR lastcolr, thiscolr; |
183 |
< |
int rept; |
184 |
< |
|
185 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
186 |
< |
lastcolr[EXP] = 0; |
187 |
< |
rept = 0; |
188 |
< |
|
189 |
< |
while (len > 0) { |
190 |
< |
setcolr(thiscolr, scanline[0][RED], |
182 |
> |
COLR *clrscan; |
183 |
> |
int n; |
184 |
> |
register COLR *sp; |
185 |
> |
/* get scanline buffer */ |
186 |
> |
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
187 |
> |
return(-1); |
188 |
> |
clrscan = sp; |
189 |
> |
/* convert scanline */ |
190 |
> |
n = len; |
191 |
> |
while (n-- > 0) { |
192 |
> |
setcolr(sp[0], scanline[0][RED], |
193 |
|
scanline[0][GRN], |
194 |
|
scanline[0][BLU]); |
255 |
– |
if (thiscolr[EXP] == lastcolr[EXP] && |
256 |
– |
thiscolr[RED] == lastcolr[RED] && |
257 |
– |
thiscolr[GRN] == lastcolr[GRN] && |
258 |
– |
thiscolr[BLU] == lastcolr[BLU]) |
259 |
– |
rept++; |
260 |
– |
else { |
261 |
– |
while (rept) { /* write out count */ |
262 |
– |
putc(1, fp); |
263 |
– |
putc(1, fp); |
264 |
– |
putc(1, fp); |
265 |
– |
putc(rept & 255, fp); |
266 |
– |
rept >>= 8; |
267 |
– |
} |
268 |
– |
putc(thiscolr[RED], fp); /* new color */ |
269 |
– |
putc(thiscolr[GRN], fp); |
270 |
– |
putc(thiscolr[BLU], fp); |
271 |
– |
putc(thiscolr[EXP], fp); |
272 |
– |
copycolr(lastcolr, thiscolr); |
273 |
– |
rept = 0; |
274 |
– |
} |
195 |
|
scanline++; |
196 |
< |
len--; |
196 |
> |
sp++; |
197 |
|
} |
198 |
< |
while (rept) { /* write out count */ |
279 |
< |
putc(1, fp); |
280 |
< |
putc(1, fp); |
281 |
< |
putc(1, fp); |
282 |
< |
putc(rept & 255, fp); |
283 |
< |
rept >>= 8; |
284 |
< |
} |
285 |
< |
return(ferror(fp) ? -1 : 0); |
198 |
> |
return(fwritecolrs(clrscan, len, fp)); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
freadscan(scanline, len, fp) /* read in a scanline */ |
203 |
|
register COLOR *scanline; |
204 |
|
int len; |
205 |
< |
register FILE *fp; |
205 |
> |
FILE *fp; |
206 |
|
{ |
207 |
< |
COLR thiscolr; |
208 |
< |
int rshift; |
209 |
< |
register int i; |
210 |
< |
|
211 |
< |
rshift = 0; |
212 |
< |
|
213 |
< |
while (len > 0) { |
214 |
< |
thiscolr[RED] = getc(fp); |
215 |
< |
thiscolr[GRN] = getc(fp); |
216 |
< |
thiscolr[BLU] = getc(fp); |
217 |
< |
thiscolr[EXP] = getc(fp); |
218 |
< |
if (feof(fp) || ferror(fp)) |
219 |
< |
return(-1); |
220 |
< |
if (thiscolr[RED] == 1 && |
221 |
< |
thiscolr[GRN] == 1 && |
222 |
< |
thiscolr[BLU] == 1) { |
223 |
< |
for (i = thiscolr[EXP] << rshift; i > 0; i--) { |
311 |
< |
copycolor(scanline[0], scanline[-1]); |
312 |
< |
scanline++; |
313 |
< |
len--; |
314 |
< |
} |
315 |
< |
rshift += 8; |
316 |
< |
} else { |
317 |
< |
colr_color(scanline[0], thiscolr); |
318 |
< |
scanline++; |
319 |
< |
len--; |
320 |
< |
rshift = 0; |
321 |
< |
} |
207 |
> |
register COLR *clrscan; |
208 |
> |
|
209 |
> |
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
210 |
> |
return(-1); |
211 |
> |
if (freadcolrs(clrscan, len, fp) < 0) |
212 |
> |
return(-1); |
213 |
> |
/* convert scanline */ |
214 |
> |
colr_color(scanline[0], clrscan[0]); |
215 |
> |
while (--len > 0) { |
216 |
> |
scanline++; clrscan++; |
217 |
> |
if (clrscan[0][RED] == clrscan[-1][RED] && |
218 |
> |
clrscan[0][GRN] == clrscan[-1][GRN] && |
219 |
> |
clrscan[0][BLU] == clrscan[-1][BLU] && |
220 |
> |
clrscan[0][EXP] == clrscan[-1][EXP]) |
221 |
> |
copycolor(scanline[0], scanline[-1]); |
222 |
> |
else |
223 |
> |
colr_color(scanline[0], clrscan[0]); |
224 |
|
} |
225 |
|
return(0); |
226 |
|
} |
230 |
|
register COLR clr; |
231 |
|
double r, g, b; |
232 |
|
{ |
331 |
– |
double frexp(); |
233 |
|
double d; |
234 |
|
int e; |
235 |
|
|
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
< |
int |
272 |
< |
colr_norm(clr, nclr) /* normalize a short color, return shift */ |
273 |
< |
COLR clr, nclr; |
271 |
> |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
272 |
> |
register COLOR c1, c2; |
273 |
> |
double md; |
274 |
|
{ |
275 |
< |
register int c; |
375 |
< |
register int shift = clr[EXP]-COLXS; |
275 |
> |
register int i; |
276 |
|
|
277 |
< |
if (shift > 0) { |
278 |
< |
if (shift >= 8) { |
279 |
< |
nclr[RED] = nclr[GRN] = nclr[BLU] = 255; |
280 |
< |
} else { |
281 |
< |
c = clr[RED] << shift; |
382 |
< |
nclr[RED] = c > 255 ? 255 : c; |
383 |
< |
c = clr[GRN] << shift; |
384 |
< |
nclr[GRN] = c > 255 ? 255 : c; |
385 |
< |
c = clr[BLU] << shift; |
386 |
< |
nclr[BLU] = c > 255 ? 255 : c; |
387 |
< |
} |
388 |
< |
} else if (shift < 0) { |
389 |
< |
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]; |
400 |
< |
} |
401 |
< |
nclr[EXP] = COLXS; |
402 |
< |
return(shift); |
277 |
> |
for (i = 0; i < 3; i++) |
278 |
> |
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || |
279 |
> |
colval(c2,i)-colval(c1,i) > md*colval(c1,i)) |
280 |
> |
return(1); |
281 |
> |
return(0); |
282 |
|
} |