1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* color.c - routines for color calculations. |
9 |
* |
10 |
* 10/10/85 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include "color.h" |
16 |
|
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 |
#ifndef frexp |
22 |
extern double frexp(); |
23 |
#endif |
24 |
|
25 |
|
26 |
char * |
27 |
tempbuffer(len) /* get a temporary buffer */ |
28 |
unsigned len; |
29 |
{ |
30 |
extern char *malloc(), *realloc(); |
31 |
static char *tempbuf = NULL; |
32 |
static unsigned tempbuflen = 0; |
33 |
|
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 |
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, j, beg, cnt; |
51 |
int c2; |
52 |
|
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 |
} |
95 |
|
96 |
|
97 |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
98 |
register COLR *scanline; |
99 |
int len; |
100 |
register FILE *fp; |
101 |
{ |
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 |
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 |
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 |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
142 |
register COLR *scanline; |
143 |
int len; |
144 |
register FILE *fp; |
145 |
{ |
146 |
int rshift; |
147 |
register int i; |
148 |
|
149 |
rshift = 0; |
150 |
|
151 |
while (len > 0) { |
152 |
scanline[0][RED] = getc(fp); |
153 |
scanline[0][GRN] = getc(fp); |
154 |
scanline[0][BLU] = getc(fp); |
155 |
scanline[0][EXP] = getc(fp); |
156 |
if (feof(fp) || ferror(fp)) |
157 |
return(-1); |
158 |
if (scanline[0][RED] == 1 && |
159 |
scanline[0][GRN] == 1 && |
160 |
scanline[0][BLU] == 1) { |
161 |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
162 |
copycolr(scanline[0], scanline[-1]); |
163 |
scanline++; |
164 |
len--; |
165 |
} |
166 |
rshift += 8; |
167 |
} else { |
168 |
scanline++; |
169 |
len--; |
170 |
rshift = 0; |
171 |
} |
172 |
} |
173 |
return(0); |
174 |
} |
175 |
|
176 |
|
177 |
fwritescan(scanline, len, fp) /* write out a scanline */ |
178 |
register COLOR *scanline; |
179 |
int len; |
180 |
FILE *fp; |
181 |
{ |
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]); |
195 |
scanline++; |
196 |
sp++; |
197 |
} |
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 |
FILE *fp; |
206 |
{ |
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 |
} |
227 |
|
228 |
|
229 |
setcolr(clr, r, g, b) /* assign a short color value */ |
230 |
register COLR clr; |
231 |
double r, g, b; |
232 |
{ |
233 |
double d; |
234 |
int e; |
235 |
|
236 |
d = r > g ? r : g; |
237 |
if (b > d) d = b; |
238 |
|
239 |
if (d <= 1e-32) { |
240 |
clr[RED] = clr[GRN] = clr[BLU] = 0; |
241 |
clr[EXP] = 0; |
242 |
return; |
243 |
} |
244 |
|
245 |
d = frexp(d, &e) * 256.0 / d; |
246 |
|
247 |
clr[RED] = r * d; |
248 |
clr[GRN] = g * d; |
249 |
clr[BLU] = b * d; |
250 |
clr[EXP] = e + COLXS; |
251 |
} |
252 |
|
253 |
|
254 |
colr_color(col, clr) /* convert short to float color */ |
255 |
register COLOR col; |
256 |
register COLR clr; |
257 |
{ |
258 |
double f; |
259 |
|
260 |
if (clr[EXP] == 0) |
261 |
col[RED] = col[GRN] = col[BLU] = 0.0; |
262 |
else { |
263 |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); |
264 |
col[RED] = (clr[RED] + 0.5)*f; |
265 |
col[GRN] = (clr[GRN] + 0.5)*f; |
266 |
col[BLU] = (clr[BLU] + 0.5)*f; |
267 |
} |
268 |
} |
269 |
|
270 |
|
271 |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
272 |
register COLOR c1, c2; |
273 |
double md; |
274 |
{ |
275 |
register int i; |
276 |
|
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 |
} |