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 |
+ |
#define getc getc_unlocked |
21 |
+ |
#define putc putc_unlocked |
22 |
+ |
#endif |
23 |
+ |
|
24 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
25 |
+ |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
26 |
|
#define MINRUN 4 /* minimum run length */ |
27 |
|
|
28 |
|
|
29 |
|
char * |
30 |
< |
tempbuffer(len) /* get a temporary buffer */ |
31 |
< |
unsigned len; |
30 |
> |
tempbuffer( /* get a temporary buffer */ |
31 |
> |
unsigned int len |
32 |
> |
) |
33 |
|
{ |
25 |
– |
extern char *malloc(), *realloc(); |
34 |
|
static char *tempbuf = NULL; |
35 |
< |
static int tempbuflen = 0; |
35 |
> |
static unsigned tempbuflen = 0; |
36 |
|
|
37 |
|
if (len > tempbuflen) { |
38 |
|
if (tempbuflen > 0) |
39 |
< |
tempbuf = realloc(tempbuf, len); |
39 |
> |
tempbuf = (char *)realloc((void *)tempbuf, len); |
40 |
|
else |
41 |
< |
tempbuf = malloc(len); |
41 |
> |
tempbuf = (char *)malloc(len); |
42 |
|
tempbuflen = tempbuf==NULL ? 0 : len; |
43 |
|
} |
44 |
|
return(tempbuf); |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
< |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
49 |
< |
register COLR *scanline; |
50 |
< |
int len; |
51 |
< |
register FILE *fp; |
48 |
> |
int |
49 |
> |
fwritecolrs( /* write out a colr scanline */ |
50 |
> |
COLR *scanline, |
51 |
> |
int len, |
52 |
> |
FILE *fp |
53 |
> |
) |
54 |
|
{ |
55 |
< |
register int i, j, beg, cnt; |
55 |
> |
int i, j, beg, cnt = 1; |
56 |
|
int c2; |
57 |
|
|
58 |
< |
if (len < MINELEN) /* too small to encode */ |
58 |
> |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ |
59 |
|
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
60 |
< |
if (len > 32767) /* too big! */ |
51 |
< |
return(-1); |
52 |
< |
putc(2, fp); /* put magic header */ |
60 |
> |
/* put magic header */ |
61 |
|
putc(2, fp); |
62 |
+ |
putc(2, fp); |
63 |
|
putc(len>>8, fp); |
64 |
|
putc(len&255, fp); |
65 |
|
/* put components seperately */ |
72 |
|
if (cnt >= MINRUN) |
73 |
|
break; /* long enough */ |
74 |
|
} |
75 |
< |
while (j < beg) { /* write out non-run(s) */ |
75 |
> |
if (beg-j > 1 && beg-j < MINRUN) { |
76 |
> |
c2 = j+1; |
77 |
> |
while (scanline[c2++][i] == scanline[j][i]) |
78 |
> |
if (c2 == beg) { /* short run */ |
79 |
> |
putc(128+beg-j, fp); |
80 |
> |
putc(scanline[j][i], fp); |
81 |
> |
j = beg; |
82 |
> |
break; |
83 |
> |
} |
84 |
> |
} |
85 |
> |
while (j < beg) { /* write out non-run */ |
86 |
|
if ((c2 = beg-j) > 128) c2 = 128; |
87 |
|
putc(c2, fp); |
88 |
|
while (c2--) |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
< |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
103 |
< |
register COLR *scanline; |
104 |
< |
int len; |
105 |
< |
register FILE *fp; |
102 |
> |
static int |
103 |
> |
oldreadcolrs( /* read in an old colr scanline */ |
104 |
> |
COLR *scanline, |
105 |
> |
int len, |
106 |
> |
FILE *fp |
107 |
> |
) |
108 |
|
{ |
109 |
< |
register int i, j; |
110 |
< |
int code; |
109 |
> |
int rshift; |
110 |
> |
int i; |
111 |
> |
|
112 |
> |
rshift = 0; |
113 |
> |
|
114 |
> |
while (len > 0) { |
115 |
> |
scanline[0][RED] = getc(fp); |
116 |
> |
scanline[0][GRN] = getc(fp); |
117 |
> |
scanline[0][BLU] = getc(fp); |
118 |
> |
scanline[0][EXP] = getc(fp); |
119 |
> |
if (feof(fp) || ferror(fp)) |
120 |
> |
return(-1); |
121 |
> |
if (scanline[0][RED] == 1 && |
122 |
> |
scanline[0][GRN] == 1 && |
123 |
> |
scanline[0][BLU] == 1) { |
124 |
> |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
125 |
> |
copycolr(scanline[0], scanline[-1]); |
126 |
> |
scanline++; |
127 |
> |
len--; |
128 |
> |
} |
129 |
> |
rshift += 8; |
130 |
> |
} else { |
131 |
> |
scanline++; |
132 |
> |
len--; |
133 |
> |
rshift = 0; |
134 |
> |
} |
135 |
> |
} |
136 |
> |
return(0); |
137 |
> |
} |
138 |
> |
|
139 |
> |
|
140 |
> |
int |
141 |
> |
freadcolrs( /* read in an encoded colr scanline */ |
142 |
> |
COLR *scanline, |
143 |
> |
int len, |
144 |
> |
FILE *fp |
145 |
> |
) |
146 |
> |
{ |
147 |
> |
int i, j; |
148 |
> |
int code, val; |
149 |
|
/* determine scanline type */ |
150 |
< |
if (len < MINELEN) |
150 |
> |
if ((len < MINELEN) | (len > MAXELEN)) |
151 |
|
return(oldreadcolrs(scanline, len, fp)); |
152 |
|
if ((i = getc(fp)) == EOF) |
153 |
|
return(-1); |
172 |
|
if ((code = getc(fp)) == EOF) |
173 |
|
return(-1); |
174 |
|
if (code > 128) { /* run */ |
175 |
< |
scanline[j++][i] = getc(fp); |
176 |
< |
for (code &= 127; --code; j++) |
177 |
< |
scanline[j][i] = scanline[j-1][i]; |
178 |
< |
} else /* non-run */ |
175 |
> |
code &= 127; |
176 |
> |
if ((val = getc(fp)) == EOF) |
177 |
> |
return -1; |
178 |
> |
if (j + code > len) |
179 |
> |
return -1; /* overrun */ |
180 |
|
while (code--) |
181 |
< |
scanline[j++][i] = getc(fp); |
182 |
< |
} |
183 |
< |
return(feof(fp) ? -1 : 0); |
184 |
< |
} |
185 |
< |
|
186 |
< |
|
187 |
< |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
188 |
< |
register COLR *scanline; |
189 |
< |
int len; |
130 |
< |
register FILE *fp; |
131 |
< |
{ |
132 |
< |
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; |
181 |
> |
scanline[j++][i] = val; |
182 |
> |
} else { /* non-run */ |
183 |
> |
if (j + code > len) |
184 |
> |
return -1; /* overrun */ |
185 |
> |
while (code--) { |
186 |
> |
if ((val = getc(fp)) == EOF) |
187 |
> |
return -1; |
188 |
> |
scanline[j++][i] = val; |
189 |
> |
} |
190 |
|
} |
191 |
< |
} |
191 |
> |
} |
192 |
|
return(0); |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
< |
fwritescan(scanline, len, fp) /* write out a scanline */ |
197 |
< |
register COLOR *scanline; |
198 |
< |
int len; |
199 |
< |
FILE *fp; |
196 |
> |
int |
197 |
> |
fwritescan( /* write out a scanline */ |
198 |
> |
COLOR *scanline, |
199 |
> |
int len, |
200 |
> |
FILE *fp |
201 |
> |
) |
202 |
|
{ |
203 |
|
COLR *clrscan; |
204 |
|
int n; |
205 |
< |
register COLR *sp; |
205 |
> |
COLR *sp; |
206 |
|
/* get scanline buffer */ |
207 |
|
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
208 |
|
return(-1); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
< |
freadscan(scanline, len, fp) /* read in a scanline */ |
224 |
< |
register COLOR *scanline; |
225 |
< |
int len; |
226 |
< |
FILE *fp; |
223 |
> |
int |
224 |
> |
freadscan( /* read in a scanline */ |
225 |
> |
COLOR *scanline, |
226 |
> |
int len, |
227 |
> |
FILE *fp |
228 |
> |
) |
229 |
|
{ |
230 |
< |
register COLR *clrscan; |
230 |
> |
COLR *clrscan; |
231 |
|
|
232 |
|
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
233 |
|
return(-1); |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
< |
setcolr(clr, r, g, b) /* assign a short color value */ |
253 |
< |
register COLR clr; |
254 |
< |
double r, g, b; |
252 |
> |
void |
253 |
> |
setcolr( /* assign a short color value */ |
254 |
> |
COLR clr, |
255 |
> |
double r, |
256 |
> |
double g, |
257 |
> |
double b |
258 |
> |
) |
259 |
|
{ |
219 |
– |
double frexp(); |
260 |
|
double d; |
261 |
|
int e; |
262 |
|
|
269 |
|
return; |
270 |
|
} |
271 |
|
|
272 |
< |
d = frexp(d, &e) * 256.0 / d; |
272 |
> |
d = frexp(d, &e) * 255.9999 / d; |
273 |
|
|
274 |
< |
clr[RED] = r * d; |
275 |
< |
clr[GRN] = g * d; |
276 |
< |
clr[BLU] = b * d; |
274 |
> |
if (r > 0.0) |
275 |
> |
clr[RED] = r * d; |
276 |
> |
else |
277 |
> |
clr[RED] = 0; |
278 |
> |
if (g > 0.0) |
279 |
> |
clr[GRN] = g * d; |
280 |
> |
else |
281 |
> |
clr[GRN] = 0; |
282 |
> |
if (b > 0.0) |
283 |
> |
clr[BLU] = b * d; |
284 |
> |
else |
285 |
> |
clr[BLU] = 0; |
286 |
> |
|
287 |
|
clr[EXP] = e + COLXS; |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
< |
colr_color(col, clr) /* convert short to float color */ |
292 |
< |
register COLOR col; |
293 |
< |
register COLR clr; |
291 |
> |
void |
292 |
> |
colr_color( /* convert short to float color */ |
293 |
> |
COLOR col, |
294 |
> |
COLR clr |
295 |
> |
) |
296 |
|
{ |
297 |
|
double f; |
298 |
|
|
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
< |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
311 |
< |
register COLOR c1, c2; |
312 |
< |
double md; |
310 |
> |
int |
311 |
> |
bigdiff( /* c1 delta c2 > md? */ |
312 |
> |
COLOR c1, |
313 |
> |
COLOR c2, |
314 |
> |
double md |
315 |
> |
) |
316 |
|
{ |
317 |
< |
register int i; |
317 |
> |
int i; |
318 |
|
|
319 |
|
for (i = 0; i < 3; i++) |
320 |
|
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) || |