1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* color.c - routines for color calculations. |
6 |
|
* |
7 |
< |
* 10/10/85 |
7 |
> |
* Externals declared in color.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
+ |
#include <stdio.h> |
13 |
+ |
#include <stdlib.h> |
14 |
+ |
#include <math.h> |
15 |
|
#include "color.h" |
16 |
|
|
17 |
+ |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
18 |
+ |
#undef getc |
19 |
+ |
#undef putc |
20 |
+ |
#undef ferror |
21 |
+ |
#define getc getc_unlocked |
22 |
+ |
#define putc putc_unlocked |
23 |
+ |
#define ferror ferror_unlocked |
24 |
+ |
#endif |
25 |
+ |
|
26 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
27 |
+ |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
28 |
|
#define MINRUN 4 /* minimum run length */ |
29 |
|
|
20 |
– |
#ifndef frexp |
21 |
– |
extern double frexp(); |
22 |
– |
#endif |
30 |
|
|
24 |
– |
|
31 |
|
char * |
32 |
< |
tempbuffer(len) /* get a temporary buffer */ |
33 |
< |
unsigned len; |
32 |
> |
tempbuffer( /* get a temporary buffer */ |
33 |
> |
unsigned int len |
34 |
> |
) |
35 |
|
{ |
29 |
– |
extern char *malloc(), *realloc(); |
36 |
|
static char *tempbuf = NULL; |
37 |
< |
static int tempbuflen = 0; |
37 |
> |
static unsigned tempbuflen = 0; |
38 |
|
|
39 |
< |
if (len > tempbuflen) { |
40 |
< |
if (tempbuflen > 0) |
41 |
< |
tempbuf = realloc(tempbuf, len); |
42 |
< |
else |
43 |
< |
tempbuf = malloc(len); |
38 |
< |
tempbuflen = tempbuf==NULL ? 0 : len; |
39 |
> |
if (!len | (len > tempbuflen)) { |
40 |
> |
if (tempbuflen) |
41 |
> |
free(tempbuf); |
42 |
> |
tempbuf = len ? (char *)malloc(len) : (char *)NULL; |
43 |
> |
tempbuflen = len*(tempbuf != NULL); |
44 |
|
} |
45 |
|
return(tempbuf); |
46 |
|
} |
47 |
|
|
48 |
|
|
49 |
< |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
50 |
< |
register COLR *scanline; |
51 |
< |
unsigned len; |
52 |
< |
register FILE *fp; |
49 |
> |
int |
50 |
> |
fwritecolrs( /* write out a colr scanline */ |
51 |
> |
COLR *scanline, |
52 |
> |
int len, |
53 |
> |
FILE *fp |
54 |
> |
) |
55 |
|
{ |
56 |
< |
register int i, j, beg, cnt; |
56 |
> |
int i, j, beg, cnt = 1; |
57 |
|
int c2; |
58 |
|
|
59 |
< |
if (len < MINELEN | len > 0x7fff) /* OOBs, write out flat */ |
59 |
> |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ |
60 |
|
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
61 |
|
/* put magic header */ |
62 |
|
putc(2, fp); |
67 |
|
for (i = 0; i < 4; i++) { |
68 |
|
for (j = 0; j < len; j += cnt) { /* find next run */ |
69 |
|
for (beg = j; beg < len; beg += cnt) { |
70 |
< |
for (cnt = 1; cnt < 127 && beg+cnt < len && |
70 |
> |
for (cnt = 1; (cnt < 127) & (beg+cnt < len) && |
71 |
|
scanline[beg+cnt][i] == scanline[beg][i]; cnt++) |
72 |
|
; |
73 |
|
if (cnt >= MINRUN) |
74 |
|
break; /* long enough */ |
75 |
|
} |
76 |
< |
if (beg-j > 1 && beg-j < MINRUN) { |
76 |
> |
if ((beg-j > 1) & (beg-j < MINRUN)) { |
77 |
|
c2 = j+1; |
78 |
|
while (scanline[c2++][i] == scanline[j][i]) |
79 |
|
if (c2 == beg) { /* short run */ |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
< |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
104 |
< |
register COLR *scanline; |
105 |
< |
int len; |
106 |
< |
register FILE *fp; |
103 |
> |
static int |
104 |
> |
oldreadcolrs( /* read in an old-style colr scanline */ |
105 |
> |
COLR *scanline, |
106 |
> |
int len, |
107 |
> |
FILE *fp |
108 |
> |
) |
109 |
|
{ |
110 |
< |
register int i, j; |
111 |
< |
int code; |
110 |
> |
int rshift = 0; |
111 |
> |
int i; |
112 |
> |
|
113 |
> |
while (len > 0) { |
114 |
> |
scanline[0][RED] = getc(fp); |
115 |
> |
scanline[0][GRN] = getc(fp); |
116 |
> |
scanline[0][BLU] = getc(fp); |
117 |
> |
scanline[0][EXP] = i = getc(fp); |
118 |
> |
if (i == EOF) |
119 |
> |
return(-1); |
120 |
> |
if (scanline[0][GRN] == 1 && |
121 |
> |
(scanline[0][RED] == 1) & |
122 |
> |
(scanline[0][BLU] == 1)) { |
123 |
> |
i = scanline[0][EXP] << rshift; |
124 |
> |
while (i--) { |
125 |
> |
copycolr(scanline[0], scanline[-1]); |
126 |
> |
if (--len <= 0) |
127 |
> |
return(0); |
128 |
> |
scanline++; |
129 |
> |
} |
130 |
> |
rshift += 8; |
131 |
> |
} else { |
132 |
> |
scanline++; |
133 |
> |
len--; |
134 |
> |
rshift = 0; |
135 |
> |
} |
136 |
> |
} |
137 |
> |
return(0); |
138 |
> |
} |
139 |
> |
|
140 |
> |
|
141 |
> |
int |
142 |
> |
freadcolrs( /* read in an encoded colr scanline */ |
143 |
> |
COLR *scanline, |
144 |
> |
int len, |
145 |
> |
FILE *fp |
146 |
> |
) |
147 |
> |
{ |
148 |
> |
int i, j; |
149 |
> |
int code, val; |
150 |
|
/* determine scanline type */ |
151 |
< |
if (len < MINELEN | len > 0x7fff) |
151 |
> |
if ((len < MINELEN) | (len > MAXELEN)) |
152 |
|
return(oldreadcolrs(scanline, len, fp)); |
153 |
|
if ((i = getc(fp)) == EOF) |
154 |
|
return(-1); |
173 |
|
if ((code = getc(fp)) == EOF) |
174 |
|
return(-1); |
175 |
|
if (code > 128) { /* run */ |
176 |
< |
scanline[j++][i] = getc(fp); |
177 |
< |
for (code &= 127; --code; j++) |
178 |
< |
scanline[j][i] = scanline[j-1][i]; |
179 |
< |
} else /* non-run */ |
176 |
> |
code &= 127; |
177 |
> |
if ((val = getc(fp)) == EOF) |
178 |
> |
return -1; |
179 |
> |
if (j + code > len) |
180 |
> |
return -1; /* overrun */ |
181 |
|
while (code--) |
182 |
< |
scanline[j++][i] = getc(fp); |
183 |
< |
} |
184 |
< |
return(feof(fp) ? -1 : 0); |
185 |
< |
} |
186 |
< |
|
187 |
< |
|
188 |
< |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
189 |
< |
register COLR *scanline; |
190 |
< |
int len; |
143 |
< |
register FILE *fp; |
144 |
< |
{ |
145 |
< |
int rshift; |
146 |
< |
register int i; |
147 |
< |
|
148 |
< |
rshift = 0; |
149 |
< |
|
150 |
< |
while (len > 0) { |
151 |
< |
scanline[0][RED] = getc(fp); |
152 |
< |
scanline[0][GRN] = getc(fp); |
153 |
< |
scanline[0][BLU] = getc(fp); |
154 |
< |
scanline[0][EXP] = getc(fp); |
155 |
< |
if (feof(fp) || ferror(fp)) |
156 |
< |
return(-1); |
157 |
< |
if (scanline[0][RED] == 1 && |
158 |
< |
scanline[0][GRN] == 1 && |
159 |
< |
scanline[0][BLU] == 1) { |
160 |
< |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
161 |
< |
copycolr(scanline[0], scanline[-1]); |
162 |
< |
scanline++; |
163 |
< |
len--; |
164 |
< |
} |
165 |
< |
rshift += 8; |
166 |
< |
} else { |
167 |
< |
scanline++; |
168 |
< |
len--; |
169 |
< |
rshift = 0; |
182 |
> |
scanline[j++][i] = val; |
183 |
> |
} else { /* non-run */ |
184 |
> |
if (j + code > len) |
185 |
> |
return -1; /* overrun */ |
186 |
> |
while (code--) { |
187 |
> |
if ((val = getc(fp)) == EOF) |
188 |
> |
return -1; |
189 |
> |
scanline[j++][i] = val; |
190 |
> |
} |
191 |
|
} |
192 |
< |
} |
192 |
> |
} |
193 |
|
return(0); |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
< |
fwritescan(scanline, len, fp) /* write out a scanline */ |
198 |
< |
register COLOR *scanline; |
199 |
< |
int len; |
200 |
< |
FILE *fp; |
197 |
> |
int |
198 |
> |
fwritescan( /* write out a scanline */ |
199 |
> |
COLOR *scanline, |
200 |
> |
int len, |
201 |
> |
FILE *fp |
202 |
> |
) |
203 |
|
{ |
204 |
|
COLR *clrscan; |
205 |
|
int n; |
206 |
< |
register COLR *sp; |
206 |
> |
COLR *sp; |
207 |
|
/* get scanline buffer */ |
208 |
|
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
209 |
|
return(-1); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
< |
freadscan(scanline, len, fp) /* read in a scanline */ |
225 |
< |
register COLOR *scanline; |
226 |
< |
int len; |
227 |
< |
FILE *fp; |
224 |
> |
int |
225 |
> |
freadscan( /* read in a scanline */ |
226 |
> |
COLOR *scanline, |
227 |
> |
int len, |
228 |
> |
FILE *fp |
229 |
> |
) |
230 |
|
{ |
231 |
< |
register COLR *clrscan; |
231 |
> |
COLR *clrscan; |
232 |
|
|
233 |
|
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
234 |
|
return(-1); |
238 |
|
colr_color(scanline[0], clrscan[0]); |
239 |
|
while (--len > 0) { |
240 |
|
scanline++; clrscan++; |
241 |
< |
if (clrscan[0][RED] == clrscan[-1][RED] && |
242 |
< |
clrscan[0][GRN] == clrscan[-1][GRN] && |
243 |
< |
clrscan[0][BLU] == clrscan[-1][BLU] && |
244 |
< |
clrscan[0][EXP] == clrscan[-1][EXP]) |
241 |
> |
if (clrscan[0][GRN] == clrscan[-1][GRN] && |
242 |
> |
(clrscan[0][RED] == clrscan[-1][RED]) & |
243 |
> |
(clrscan[0][BLU] == clrscan[-1][BLU]) & |
244 |
> |
(clrscan[0][EXP] == clrscan[-1][EXP])) |
245 |
|
copycolor(scanline[0], scanline[-1]); |
246 |
|
else |
247 |
|
colr_color(scanline[0], clrscan[0]); |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
< |
setcolr(clr, r, g, b) /* assign a short color value */ |
254 |
< |
register COLR clr; |
255 |
< |
double r, g, b; |
253 |
> |
void |
254 |
> |
setcolr( /* assign a short color value */ |
255 |
> |
COLR clr, |
256 |
> |
double r, |
257 |
> |
double g, |
258 |
> |
double b |
259 |
> |
) |
260 |
|
{ |
261 |
|
double d; |
262 |
|
int e; |
270 |
|
return; |
271 |
|
} |
272 |
|
|
273 |
< |
d = frexp(d, &e) * 256.0 / d; |
273 |
> |
d = frexp(d, &e) * 255.9999 / d; |
274 |
|
|
275 |
< |
clr[RED] = r * d; |
276 |
< |
clr[GRN] = g * d; |
277 |
< |
clr[BLU] = b * d; |
275 |
> |
if (r > 0.0) |
276 |
> |
clr[RED] = r * d; |
277 |
> |
else |
278 |
> |
clr[RED] = 0; |
279 |
> |
if (g > 0.0) |
280 |
> |
clr[GRN] = g * d; |
281 |
> |
else |
282 |
> |
clr[GRN] = 0; |
283 |
> |
if (b > 0.0) |
284 |
> |
clr[BLU] = b * d; |
285 |
> |
else |
286 |
> |
clr[BLU] = 0; |
287 |
> |
|
288 |
|
clr[EXP] = e + COLXS; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
< |
colr_color(col, clr) /* convert short to float color */ |
293 |
< |
register COLOR col; |
294 |
< |
register COLR clr; |
292 |
> |
void |
293 |
> |
colr_color( /* convert short to float color */ |
294 |
> |
COLOR col, |
295 |
> |
COLR clr |
296 |
> |
) |
297 |
|
{ |
298 |
|
double f; |
299 |
|
|
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
< |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
312 |
< |
register COLOR c1, c2; |
313 |
< |
double md; |
311 |
> |
int |
312 |
> |
bigdiff( /* c1 delta c2 > md? */ |
313 |
> |
COLOR c1, |
314 |
> |
COLOR c2, |
315 |
> |
double md |
316 |
> |
) |
317 |
|
{ |
318 |
< |
register int i; |
318 |
> |
int i; |
319 |
|
|
320 |
|
for (i = 0; i < 3; i++) |
321 |
|
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || |
322 |
< |
colval(c2,i)-colval(c1,i) > md*colval(c1,i)) |
322 |
> |
colval(c2,i)-colval(c1,i) > md*colval(c1,i)) |
323 |
|
return(1); |
324 |
|
return(0); |
325 |
|
} |