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 MINRUN 4 /* minimum run length */ |
19 |
|
|
24 |
– |
#define STARTWL 380 /* starting wavelength (nanometers) */ |
25 |
– |
#define INCWL 10 /* wavelength increment */ |
26 |
– |
#define NINC 40 /* # of values */ |
20 |
|
|
21 |
< |
static BYTE chroma[3][NINC] = { |
22 |
< |
{ /* X */ |
23 |
< |
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) /* compute RGB color from spectral range */ |
49 |
< |
COLOR col; |
50 |
< |
int s, e; |
21 |
> |
char * |
22 |
> |
tempbuffer(len) /* get a temporary buffer */ |
23 |
> |
unsigned len; |
24 |
|
{ |
25 |
< |
COLOR ciecolor; |
25 |
> |
extern char *malloc(), *realloc(); |
26 |
> |
static char *tempbuf = NULL; |
27 |
> |
static int tempbuflen = 0; |
28 |
|
|
29 |
< |
spec_cie(ciecolor, s, e); |
30 |
< |
cie_rgb(col, ciecolor); |
29 |
> |
if (len > tempbuflen) { |
30 |
> |
if (tempbuflen > 0) |
31 |
> |
tempbuf = realloc(tempbuf, len); |
32 |
> |
else |
33 |
> |
tempbuf = malloc(len); |
34 |
> |
tempbuflen = tempbuf==NULL ? 0 : len; |
35 |
> |
} |
36 |
> |
return(tempbuf); |
37 |
|
} |
38 |
|
|
39 |
|
|
40 |
< |
spec_cie(col, s, e) /* compute a color from a spectral range */ |
41 |
< |
COLOR col; /* returned color */ |
42 |
< |
int s, e; /* starting and ending wavelengths */ |
40 |
> |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
41 |
> |
register COLR *scanline; |
42 |
> |
int len; |
43 |
> |
register FILE *fp; |
44 |
|
{ |
45 |
< |
register int i, d, r; |
45 |
> |
register int i, j, beg, cnt; |
46 |
> |
int c2; |
47 |
|
|
48 |
< |
s -= STARTWL; |
49 |
< |
if (s < 0) |
50 |
< |
s = 0; |
51 |
< |
|
52 |
< |
e -= STARTWL; |
53 |
< |
if (e >= INCWL*(NINC - 1)) |
54 |
< |
e = INCWL*(NINC - 1) - 1; |
55 |
< |
|
56 |
< |
d = e / INCWL; /* interpolate values */ |
57 |
< |
r = e % INCWL; |
58 |
< |
for (i = 0; i < 3; i++) |
59 |
< |
col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r; |
60 |
< |
|
61 |
< |
d = s / INCWL; |
62 |
< |
r = s % INCWL; |
63 |
< |
for (i = 0; i < 3; i++) |
64 |
< |
col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r; |
65 |
< |
|
66 |
< |
col[RED] = (col[RED] + 0.5) / (256*INCWL); |
67 |
< |
col[GRN] = (col[GRN] + 0.5) / (256*INCWL); |
68 |
< |
col[BLU] = (col[BLU] + 0.5) / (256*INCWL); |
69 |
< |
} |
70 |
< |
|
71 |
< |
|
72 |
< |
cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ |
73 |
< |
register COLOR rgbcolor, ciecolor; |
74 |
< |
{ |
75 |
< |
static float cmat[3][3] = { |
76 |
< |
1.73, -.48, -.26, |
77 |
< |
-.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; |
48 |
> |
if (len < MINELEN) /* too small to encode */ |
49 |
> |
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
50 |
> |
if (len > 32767) /* too big! */ |
51 |
> |
return(-1); |
52 |
> |
putc(2, fp); /* put magic header */ |
53 |
> |
putc(2, fp); |
54 |
> |
putc(len>>8, fp); |
55 |
> |
putc(len&255, fp); |
56 |
> |
/* put components seperately */ |
57 |
> |
for (i = 0; i < 4; i++) { |
58 |
> |
for (j = 0; j < len; j += cnt) { /* find next run */ |
59 |
> |
for (beg = j; beg < len; beg += cnt) { |
60 |
> |
for (cnt = 1; cnt < 127 && beg+cnt < len && |
61 |
> |
scanline[beg+cnt][i] == scanline[beg][i]; cnt++) |
62 |
> |
; |
63 |
> |
if (cnt >= MINRUN) |
64 |
> |
break; /* long enough */ |
65 |
> |
} |
66 |
> |
while (j < beg) { /* write out non-run(s) */ |
67 |
> |
if ((c2 = beg-j) > 128) c2 = 128; |
68 |
> |
putc(c2, fp); |
69 |
> |
while (c2--) |
70 |
> |
putc(scanline[j++][i], fp); |
71 |
> |
} |
72 |
> |
if (cnt >= MINRUN) { /* write out run */ |
73 |
> |
putc(128+cnt, fp); |
74 |
> |
putc(scanline[beg][i], fp); |
75 |
> |
} else |
76 |
> |
cnt = 0; |
77 |
> |
} |
78 |
|
} |
79 |
+ |
return(ferror(fp) ? -1 : 0); |
80 |
|
} |
107 |
– |
#endif |
81 |
|
|
82 |
|
|
83 |
< |
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 */ |
83 |
> |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
84 |
|
register COLR *scanline; |
85 |
|
int len; |
86 |
|
register FILE *fp; |
87 |
|
{ |
88 |
< |
COLR lastcolr; |
89 |
< |
int rept; |
90 |
< |
|
91 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
92 |
< |
lastcolr[EXP] = 0; |
93 |
< |
rept = 0; |
94 |
< |
|
95 |
< |
while (len > 0) { |
96 |
< |
if (scanline[0][EXP] == lastcolr[EXP] && |
97 |
< |
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--; |
88 |
> |
register int i, j; |
89 |
> |
int code; |
90 |
> |
/* determine scanline type */ |
91 |
> |
if (len < MINELEN) |
92 |
> |
return(oldreadcolrs(scanline, len, fp)); |
93 |
> |
if ((i = getc(fp)) == EOF) |
94 |
> |
return(-1); |
95 |
> |
if (i != 2) { |
96 |
> |
ungetc(i, fp); |
97 |
> |
return(oldreadcolrs(scanline, len, fp)); |
98 |
|
} |
99 |
< |
while (rept) { /* write out count */ |
100 |
< |
putc(1, fp); |
101 |
< |
putc(1, fp); |
102 |
< |
putc(1, fp); |
103 |
< |
putc(rept & 255, fp); |
104 |
< |
rept >>= 8; |
99 |
> |
scanline[0][GRN] = getc(fp); |
100 |
> |
scanline[0][BLU] = getc(fp); |
101 |
> |
if ((i = getc(fp)) == EOF) |
102 |
> |
return(-1); |
103 |
> |
if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) { |
104 |
> |
scanline[0][RED] = 2; |
105 |
> |
scanline[0][EXP] = i; |
106 |
> |
return(oldreadcolrs(scanline+1, len-1, fp)); |
107 |
|
} |
108 |
< |
return(ferror(fp) ? -1 : 0); |
108 |
> |
if ((scanline[0][BLU]<<8 | i) != len) |
109 |
> |
return(-1); /* length mismatch! */ |
110 |
> |
/* read each component */ |
111 |
> |
for (i = 0; i < 4; i++) |
112 |
> |
for (j = 0; j < len; ) { |
113 |
> |
if ((code = getc(fp)) == EOF) |
114 |
> |
return(-1); |
115 |
> |
if (code > 128) { /* run */ |
116 |
> |
scanline[j++][i] = getc(fp); |
117 |
> |
for (code &= 127; --code; j++) |
118 |
> |
scanline[j][i] = scanline[j-1][i]; |
119 |
> |
} else /* non-run */ |
120 |
> |
while (code--) |
121 |
> |
scanline[j++][i] = getc(fp); |
122 |
> |
} |
123 |
> |
return(feof(fp) ? -1 : 0); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
< |
freadcolrs(scanline, len, fp) /* read in a colr scanline */ |
127 |
> |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
128 |
|
register COLR *scanline; |
129 |
|
int len; |
130 |
|
register FILE *fp; |
163 |
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
164 |
|
register COLOR *scanline; |
165 |
|
int len; |
166 |
< |
register FILE *fp; |
166 |
> |
FILE *fp; |
167 |
|
{ |
168 |
< |
COLR lastcolr, thiscolr; |
169 |
< |
int rept; |
170 |
< |
|
171 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
172 |
< |
lastcolr[EXP] = 0; |
173 |
< |
rept = 0; |
174 |
< |
|
175 |
< |
while (len > 0) { |
176 |
< |
setcolr(thiscolr, scanline[0][RED], |
168 |
> |
COLR *clrscan; |
169 |
> |
int n; |
170 |
> |
register COLR *sp; |
171 |
> |
/* get scanline buffer */ |
172 |
> |
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
173 |
> |
return(-1); |
174 |
> |
clrscan = sp; |
175 |
> |
/* convert scanline */ |
176 |
> |
n = len; |
177 |
> |
while (n-- > 0) { |
178 |
> |
setcolr(sp[0], scanline[0][RED], |
179 |
|
scanline[0][GRN], |
180 |
|
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 |
– |
} |
181 |
|
scanline++; |
182 |
< |
len--; |
182 |
> |
sp++; |
183 |
|
} |
184 |
< |
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); |
184 |
> |
return(fwritecolrs(clrscan, len, fp)); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
freadscan(scanline, len, fp) /* read in a scanline */ |
189 |
|
register COLOR *scanline; |
190 |
|
int len; |
191 |
< |
register FILE *fp; |
191 |
> |
FILE *fp; |
192 |
|
{ |
193 |
< |
COLR thiscolr; |
194 |
< |
int rshift; |
195 |
< |
register int i; |
196 |
< |
|
197 |
< |
rshift = 0; |
198 |
< |
|
199 |
< |
while (len > 0) { |
200 |
< |
thiscolr[RED] = getc(fp); |
201 |
< |
thiscolr[GRN] = getc(fp); |
202 |
< |
thiscolr[BLU] = getc(fp); |
203 |
< |
thiscolr[EXP] = getc(fp); |
204 |
< |
if (feof(fp) || ferror(fp)) |
205 |
< |
return(-1); |
206 |
< |
if (thiscolr[RED] == 1 && |
207 |
< |
thiscolr[GRN] == 1 && |
208 |
< |
thiscolr[BLU] == 1) { |
209 |
< |
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 |
< |
} |
193 |
> |
register COLR *clrscan; |
194 |
> |
|
195 |
> |
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
196 |
> |
return(-1); |
197 |
> |
if (freadcolrs(clrscan, len, fp) < 0) |
198 |
> |
return(-1); |
199 |
> |
/* convert scanline */ |
200 |
> |
colr_color(scanline[0], clrscan[0]); |
201 |
> |
while (--len > 0) { |
202 |
> |
scanline++; clrscan++; |
203 |
> |
if (clrscan[0][RED] == clrscan[-1][RED] && |
204 |
> |
clrscan[0][GRN] == clrscan[-1][GRN] && |
205 |
> |
clrscan[0][BLU] == clrscan[-1][BLU] && |
206 |
> |
clrscan[0][EXP] == clrscan[-1][EXP]) |
207 |
> |
copycolor(scanline[0], scanline[-1]); |
208 |
> |
else |
209 |
> |
colr_color(scanline[0], clrscan[0]); |
210 |
|
} |
211 |
|
return(0); |
212 |
|
} |
251 |
|
col[RED] = (clr[RED] + 0.5)*f; |
252 |
|
col[GRN] = (clr[GRN] + 0.5)*f; |
253 |
|
col[BLU] = (clr[BLU] + 0.5)*f; |
366 |
– |
} |
367 |
– |
} |
368 |
– |
|
369 |
– |
|
370 |
– |
normcolrs(scan, len, adjust) /* normalize a scanline of colrs */ |
371 |
– |
register COLR *scan; |
372 |
– |
int len; |
373 |
– |
int adjust; |
374 |
– |
{ |
375 |
– |
register int c; |
376 |
– |
register int shift; |
377 |
– |
|
378 |
– |
while (len-- > 0) { |
379 |
– |
shift = scan[0][EXP] + adjust - COLXS; |
380 |
– |
if (shift > 0) { |
381 |
– |
if (shift > 8) { |
382 |
– |
scan[0][RED] = |
383 |
– |
scan[0][GRN] = |
384 |
– |
scan[0][BLU] = 255; |
385 |
– |
} else { |
386 |
– |
shift--; |
387 |
– |
c = (scan[0][RED]<<1 | 1) << shift; |
388 |
– |
scan[0][RED] = c > 255 ? 255 : c; |
389 |
– |
c = (scan[0][GRN]<<1 | 1) << shift; |
390 |
– |
scan[0][GRN] = c > 255 ? 255 : c; |
391 |
– |
c = (scan[0][BLU]<<1 | 1) << shift; |
392 |
– |
scan[0][BLU] = c > 255 ? 255 : c; |
393 |
– |
} |
394 |
– |
} else if (shift < 0) { |
395 |
– |
if (shift < -8) { |
396 |
– |
scan[0][RED] = |
397 |
– |
scan[0][GRN] = |
398 |
– |
scan[0][BLU] = 0; |
399 |
– |
} else { |
400 |
– |
shift = -1-shift; |
401 |
– |
scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1; |
402 |
– |
scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1; |
403 |
– |
scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1; |
404 |
– |
} |
405 |
– |
} |
406 |
– |
scan[0][EXP] = COLXS - adjust; |
407 |
– |
scan++; |
254 |
|
} |
255 |
|
} |
256 |
|
|