1 |
greg |
1.14 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
greg |
1.1 |
|
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 |
greg |
1.14 |
#define MINELEN 8 /* minimum scanline length for encoding */ |
18 |
|
|
#define MINRUN 4 /* minimum run length */ |
19 |
greg |
1.5 |
|
20 |
greg |
1.14 |
|
21 |
|
|
char * |
22 |
|
|
tempbuffer(len) /* get a temporary buffer */ |
23 |
|
|
unsigned len; |
24 |
|
|
{ |
25 |
|
|
extern char *malloc(), *realloc(); |
26 |
|
|
static char *tempbuf = NULL; |
27 |
|
|
static int tempbuflen = 0; |
28 |
|
|
|
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 |
greg |
1.1 |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
41 |
|
|
register COLR *scanline; |
42 |
|
|
int len; |
43 |
|
|
register FILE *fp; |
44 |
|
|
{ |
45 |
greg |
1.14 |
register int i, j, beg, cnt; |
46 |
|
|
int c2; |
47 |
greg |
1.1 |
|
48 |
greg |
1.14 |
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 |
greg |
1.1 |
} |
66 |
greg |
1.14 |
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 |
greg |
1.1 |
} |
79 |
|
|
return(ferror(fp) ? -1 : 0); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
greg |
1.14 |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
84 |
greg |
1.1 |
register COLR *scanline; |
85 |
|
|
int len; |
86 |
|
|
register FILE *fp; |
87 |
|
|
{ |
88 |
greg |
1.14 |
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 |
|
|
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 |
|
|
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 |
|
|
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
128 |
|
|
register COLR *scanline; |
129 |
|
|
int len; |
130 |
|
|
register FILE *fp; |
131 |
|
|
{ |
132 |
greg |
1.1 |
int rshift; |
133 |
|
|
register int i; |
134 |
|
|
|
135 |
|
|
rshift = 0; |
136 |
|
|
|
137 |
|
|
while (len > 0) { |
138 |
|
|
scanline[0][RED] = getc(fp); |
139 |
|
|
scanline[0][GRN] = getc(fp); |
140 |
|
|
scanline[0][BLU] = getc(fp); |
141 |
|
|
scanline[0][EXP] = getc(fp); |
142 |
|
|
if (feof(fp) || ferror(fp)) |
143 |
|
|
return(-1); |
144 |
|
|
if (scanline[0][RED] == 1 && |
145 |
|
|
scanline[0][GRN] == 1 && |
146 |
|
|
scanline[0][BLU] == 1) { |
147 |
|
|
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
148 |
|
|
copycolr(scanline[0], scanline[-1]); |
149 |
|
|
scanline++; |
150 |
|
|
len--; |
151 |
|
|
} |
152 |
|
|
rshift += 8; |
153 |
|
|
} else { |
154 |
|
|
scanline++; |
155 |
|
|
len--; |
156 |
|
|
rshift = 0; |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
return(0); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
164 |
|
|
register COLOR *scanline; |
165 |
|
|
int len; |
166 |
greg |
1.14 |
FILE *fp; |
167 |
greg |
1.1 |
{ |
168 |
greg |
1.14 |
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 |
greg |
1.1 |
scanline[0][GRN], |
180 |
|
|
scanline[0][BLU]); |
181 |
|
|
scanline++; |
182 |
greg |
1.14 |
sp++; |
183 |
greg |
1.1 |
} |
184 |
greg |
1.14 |
return(fwritecolrs(clrscan, len, fp)); |
185 |
greg |
1.1 |
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
freadscan(scanline, len, fp) /* read in a scanline */ |
189 |
|
|
register COLOR *scanline; |
190 |
|
|
int len; |
191 |
greg |
1.14 |
FILE *fp; |
192 |
greg |
1.1 |
{ |
193 |
greg |
1.14 |
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 |
greg |
1.1 |
} |
211 |
|
|
return(0); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
|
215 |
|
|
setcolr(clr, r, g, b) /* assign a short color value */ |
216 |
|
|
register COLR clr; |
217 |
|
|
double r, g, b; |
218 |
|
|
{ |
219 |
|
|
double frexp(); |
220 |
|
|
double d; |
221 |
|
|
int e; |
222 |
|
|
|
223 |
|
|
d = r > g ? r : g; |
224 |
|
|
if (b > d) d = b; |
225 |
|
|
|
226 |
greg |
1.4 |
if (d <= 1e-32) { |
227 |
greg |
1.1 |
clr[RED] = clr[GRN] = clr[BLU] = 0; |
228 |
|
|
clr[EXP] = 0; |
229 |
|
|
return; |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
d = frexp(d, &e) * 256.0 / d; |
233 |
|
|
|
234 |
|
|
clr[RED] = r * d; |
235 |
|
|
clr[GRN] = g * d; |
236 |
|
|
clr[BLU] = b * d; |
237 |
|
|
clr[EXP] = e + COLXS; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
|
|
colr_color(col, clr) /* convert short to float color */ |
242 |
|
|
register COLOR col; |
243 |
|
|
register COLR clr; |
244 |
|
|
{ |
245 |
greg |
1.6 |
double f; |
246 |
greg |
1.1 |
|
247 |
|
|
if (clr[EXP] == 0) |
248 |
|
|
col[RED] = col[GRN] = col[BLU] = 0.0; |
249 |
|
|
else { |
250 |
greg |
1.2 |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); |
251 |
greg |
1.1 |
col[RED] = (clr[RED] + 0.5)*f; |
252 |
|
|
col[GRN] = (clr[GRN] + 0.5)*f; |
253 |
|
|
col[BLU] = (clr[BLU] + 0.5)*f; |
254 |
|
|
} |
255 |
greg |
1.6 |
} |
256 |
|
|
|
257 |
|
|
|
258 |
greg |
1.7 |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
259 |
|
|
register COLOR c1, c2; |
260 |
|
|
double md; |
261 |
|
|
{ |
262 |
|
|
register int i; |
263 |
|
|
|
264 |
|
|
for (i = 0; i < 3; i++) |
265 |
|
|
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || |
266 |
|
|
colval(c2,i)-colval(c1,i) > md*colval(c1,i)) |
267 |
|
|
return(1); |
268 |
|
|
return(0); |
269 |
|
|
} |