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 |
+ |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
21 |
+ |
#define getc getc_unlocked |
22 |
+ |
#define putc putc_unlocked |
23 |
+ |
#endif |
24 |
+ |
|
25 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
26 |
|
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
27 |
|
#define MINRUN 4 /* minimum run length */ |
29 |
|
|
30 |
|
char * |
31 |
|
tempbuffer(len) /* get a temporary buffer */ |
32 |
< |
unsigned len; |
32 |
> |
unsigned int len; |
33 |
|
{ |
28 |
– |
extern char *malloc(), *realloc(); |
34 |
|
static char *tempbuf = NULL; |
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 |
+ |
int |
49 |
|
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
50 |
|
register COLR *scanline; |
51 |
< |
unsigned len; |
51 |
> |
int len; |
52 |
|
register FILE *fp; |
53 |
|
{ |
54 |
< |
register int i, j, beg, cnt; |
54 |
> |
register int i, j, beg, cnt = 1; |
55 |
|
int c2; |
56 |
|
|
57 |
< |
if (len < MINELEN | len > MAXELEN) /* OOBs, write out flat */ |
57 |
> |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ |
58 |
|
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
59 |
|
/* put magic header */ |
60 |
|
putc(2, fp); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
+ |
static int |
102 |
+ |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
103 |
+ |
register COLR *scanline; |
104 |
+ |
int len; |
105 |
+ |
register FILE *fp; |
106 |
+ |
{ |
107 |
+ |
int rshift; |
108 |
+ |
register int i; |
109 |
+ |
|
110 |
+ |
rshift = 0; |
111 |
+ |
|
112 |
+ |
while (len > 0) { |
113 |
+ |
scanline[0][RED] = getc(fp); |
114 |
+ |
scanline[0][GRN] = getc(fp); |
115 |
+ |
scanline[0][BLU] = getc(fp); |
116 |
+ |
scanline[0][EXP] = getc(fp); |
117 |
+ |
if (feof(fp) || ferror(fp)) |
118 |
+ |
return(-1); |
119 |
+ |
if (scanline[0][RED] == 1 && |
120 |
+ |
scanline[0][GRN] == 1 && |
121 |
+ |
scanline[0][BLU] == 1) { |
122 |
+ |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
123 |
+ |
copycolr(scanline[0], scanline[-1]); |
124 |
+ |
scanline++; |
125 |
+ |
len--; |
126 |
+ |
} |
127 |
+ |
rshift += 8; |
128 |
+ |
} else { |
129 |
+ |
scanline++; |
130 |
+ |
len--; |
131 |
+ |
rshift = 0; |
132 |
+ |
} |
133 |
+ |
} |
134 |
+ |
return(0); |
135 |
+ |
} |
136 |
+ |
|
137 |
+ |
|
138 |
+ |
int |
139 |
|
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
140 |
|
register COLR *scanline; |
141 |
|
int len; |
144 |
|
register int i, j; |
145 |
|
int code, val; |
146 |
|
/* determine scanline type */ |
147 |
< |
if (len < MINELEN | len > MAXELEN) |
147 |
> |
if ((len < MINELEN) | (len > MAXELEN)) |
148 |
|
return(oldreadcolrs(scanline, len, fp)); |
149 |
|
if ((i = getc(fp)) == EOF) |
150 |
|
return(-1); |
170 |
|
return(-1); |
171 |
|
if (code > 128) { /* run */ |
172 |
|
code &= 127; |
173 |
< |
val = getc(fp); |
173 |
> |
if ((val = getc(fp)) == EOF) |
174 |
> |
return -1; |
175 |
|
while (code--) |
176 |
|
scanline[j++][i] = val; |
177 |
|
} else /* non-run */ |
178 |
< |
while (code--) |
179 |
< |
scanline[j++][i] = getc(fp); |
178 |
> |
while (code--) { |
179 |
> |
if ((val = getc(fp)) == EOF) |
180 |
> |
return -1; |
181 |
> |
scanline[j++][i] = val; |
182 |
> |
} |
183 |
|
} |
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 |
– |
} |
184 |
|
return(0); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
+ |
int |
189 |
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
190 |
|
register COLOR *scanline; |
191 |
|
int len; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
+ |
int |
215 |
|
freadscan(scanline, len, fp) /* read in a scanline */ |
216 |
|
register COLOR *scanline; |
217 |
|
int len; |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
+ |
void |
243 |
|
setcolr(clr, r, g, b) /* assign a short color value */ |
244 |
|
register COLR clr; |
245 |
|
double r, g, b; |
256 |
|
return; |
257 |
|
} |
258 |
|
|
259 |
< |
d = frexp(d, &e) * 256.0 / d; |
259 |
> |
d = frexp(d, &e) * 255.9999 / d; |
260 |
|
|
261 |
< |
clr[RED] = r * d; |
262 |
< |
clr[GRN] = g * d; |
263 |
< |
clr[BLU] = b * d; |
261 |
> |
if (r > 0.0) |
262 |
> |
clr[RED] = r * d; |
263 |
> |
else |
264 |
> |
clr[RED] = 0; |
265 |
> |
if (g > 0.0) |
266 |
> |
clr[GRN] = g * d; |
267 |
> |
else |
268 |
> |
clr[GRN] = 0; |
269 |
> |
if (b > 0.0) |
270 |
> |
clr[BLU] = b * d; |
271 |
> |
else |
272 |
> |
clr[BLU] = 0; |
273 |
> |
|
274 |
|
clr[EXP] = e + COLXS; |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
+ |
void |
279 |
|
colr_color(col, clr) /* convert short to float color */ |
280 |
|
register COLOR col; |
281 |
|
register COLR clr; |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
+ |
int |
297 |
|
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
298 |
|
register COLOR c1, c2; |
299 |
|
double md; |