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 "copyright.h" |
11 |
+ |
|
12 |
|
#include <stdio.h> |
13 |
|
|
14 |
+ |
#include <stdlib.h> |
15 |
+ |
|
16 |
+ |
#include <math.h> |
17 |
+ |
|
18 |
|
#include "color.h" |
19 |
|
|
20 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
21 |
+ |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
22 |
|
#define MINRUN 4 /* minimum run length */ |
23 |
|
|
20 |
– |
#ifndef frexp |
21 |
– |
extern double frexp(); |
22 |
– |
#endif |
24 |
|
|
24 |
– |
|
25 |
|
char * |
26 |
|
tempbuffer(len) /* get a temporary buffer */ |
27 |
< |
unsigned len; |
27 |
> |
unsigned int len; |
28 |
|
{ |
29 |
– |
extern char *malloc(), *realloc(); |
29 |
|
static char *tempbuf = NULL; |
30 |
< |
static int tempbuflen = 0; |
30 |
> |
static unsigned tempbuflen = 0; |
31 |
|
|
32 |
|
if (len > tempbuflen) { |
33 |
|
if (tempbuflen > 0) |
34 |
< |
tempbuf = realloc(tempbuf, len); |
34 |
> |
tempbuf = (char *)realloc((void *)tempbuf, len); |
35 |
|
else |
36 |
< |
tempbuf = malloc(len); |
36 |
> |
tempbuf = (char *)malloc(len); |
37 |
|
tempbuflen = tempbuf==NULL ? 0 : len; |
38 |
|
} |
39 |
|
return(tempbuf); |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
+ |
int |
44 |
|
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
45 |
|
register COLR *scanline; |
46 |
< |
unsigned len; |
46 |
> |
int len; |
47 |
|
register FILE *fp; |
48 |
|
{ |
49 |
< |
register int i, j, beg, cnt; |
49 |
> |
register int i, j, beg, cnt = 1; |
50 |
|
int c2; |
51 |
|
|
52 |
< |
if (len < MINELEN | len > 0x7fff) /* OOBs, write out flat */ |
52 |
> |
if (len < MINELEN | len > MAXELEN) /* OOBs, write out flat */ |
53 |
|
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
54 |
|
/* put magic header */ |
55 |
|
putc(2, fp); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
+ |
static int |
97 |
+ |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
98 |
+ |
register COLR *scanline; |
99 |
+ |
int len; |
100 |
+ |
register FILE *fp; |
101 |
+ |
{ |
102 |
+ |
int rshift; |
103 |
+ |
register int i; |
104 |
+ |
|
105 |
+ |
rshift = 0; |
106 |
+ |
|
107 |
+ |
while (len > 0) { |
108 |
+ |
scanline[0][RED] = getc(fp); |
109 |
+ |
scanline[0][GRN] = getc(fp); |
110 |
+ |
scanline[0][BLU] = getc(fp); |
111 |
+ |
scanline[0][EXP] = getc(fp); |
112 |
+ |
if (feof(fp) || ferror(fp)) |
113 |
+ |
return(-1); |
114 |
+ |
if (scanline[0][RED] == 1 && |
115 |
+ |
scanline[0][GRN] == 1 && |
116 |
+ |
scanline[0][BLU] == 1) { |
117 |
+ |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
118 |
+ |
copycolr(scanline[0], scanline[-1]); |
119 |
+ |
scanline++; |
120 |
+ |
len--; |
121 |
+ |
} |
122 |
+ |
rshift += 8; |
123 |
+ |
} else { |
124 |
+ |
scanline++; |
125 |
+ |
len--; |
126 |
+ |
rshift = 0; |
127 |
+ |
} |
128 |
+ |
} |
129 |
+ |
return(0); |
130 |
+ |
} |
131 |
+ |
|
132 |
+ |
|
133 |
+ |
int |
134 |
|
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
135 |
|
register COLR *scanline; |
136 |
|
int len; |
137 |
|
register FILE *fp; |
138 |
|
{ |
139 |
|
register int i, j; |
140 |
< |
int code; |
140 |
> |
int code, val; |
141 |
|
/* determine scanline type */ |
142 |
< |
if (len < MINELEN | len > 0x7fff) |
142 |
> |
if (len < MINELEN | len > MAXELEN) |
143 |
|
return(oldreadcolrs(scanline, len, fp)); |
144 |
|
if ((i = getc(fp)) == EOF) |
145 |
|
return(-1); |
164 |
|
if ((code = getc(fp)) == EOF) |
165 |
|
return(-1); |
166 |
|
if (code > 128) { /* run */ |
167 |
< |
scanline[j++][i] = getc(fp); |
168 |
< |
for (code &= 127; --code; j++) |
169 |
< |
scanline[j][i] = scanline[j-1][i]; |
132 |
< |
} else /* non-run */ |
167 |
> |
code &= 127; |
168 |
> |
if ((val = getc(fp)) == EOF) |
169 |
> |
return -1; |
170 |
|
while (code--) |
171 |
< |
scanline[j++][i] = getc(fp); |
171 |
> |
scanline[j++][i] = val; |
172 |
> |
} else /* non-run */ |
173 |
> |
while (code--) { |
174 |
> |
if ((val = getc(fp)) == EOF) |
175 |
> |
return -1; |
176 |
> |
scanline[j++][i] = val; |
177 |
> |
} |
178 |
|
} |
136 |
– |
return(feof(fp) ? -1 : 0); |
137 |
– |
} |
138 |
– |
|
139 |
– |
|
140 |
– |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
141 |
– |
register COLR *scanline; |
142 |
– |
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; |
170 |
– |
} |
171 |
– |
} |
179 |
|
return(0); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
+ |
int |
184 |
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
185 |
|
register COLOR *scanline; |
186 |
|
int len; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
+ |
int |
210 |
|
freadscan(scanline, len, fp) /* read in a scanline */ |
211 |
|
register COLOR *scanline; |
212 |
|
int len; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
+ |
void |
238 |
|
setcolr(clr, r, g, b) /* assign a short color value */ |
239 |
|
register COLR clr; |
240 |
|
double r, g, b; |
251 |
|
return; |
252 |
|
} |
253 |
|
|
254 |
< |
d = frexp(d, &e) * 256.0 / d; |
254 |
> |
d = frexp(d, &e) * 255.9999 / d; |
255 |
|
|
256 |
< |
clr[RED] = r * d; |
257 |
< |
clr[GRN] = g * d; |
258 |
< |
clr[BLU] = b * d; |
256 |
> |
if (r > 0.0) |
257 |
> |
clr[RED] = r * d; |
258 |
> |
else |
259 |
> |
clr[RED] = 0; |
260 |
> |
if (g > 0.0) |
261 |
> |
clr[GRN] = g * d; |
262 |
> |
else |
263 |
> |
clr[GRN] = 0; |
264 |
> |
if (b > 0.0) |
265 |
> |
clr[BLU] = b * d; |
266 |
> |
else |
267 |
> |
clr[BLU] = 0; |
268 |
> |
|
269 |
|
clr[EXP] = e + COLXS; |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
+ |
void |
274 |
|
colr_color(col, clr) /* convert short to float color */ |
275 |
|
register COLOR col; |
276 |
|
register COLR clr; |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
+ |
int |
292 |
|
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
293 |
|
register COLOR c1, c2; |
294 |
|
double md; |