1 |
– |
/* Copyright (c) 1986 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 SPEC_RGB |
18 |
< |
/* |
19 |
< |
* The following table contains the CIE tristimulus integrals |
20 |
< |
* for X, Y, and Z. The table is cumulative, so that |
21 |
< |
* each color coordinate integrates to 1. |
22 |
< |
*/ |
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 STARTWL 380 /* starting wavelength (nanometers) */ |
27 |
< |
#define INCWL 10 /* wavelength increment */ |
28 |
< |
#define NINC 40 /* # of values */ |
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 |
|
|
30 |
< |
static BYTE chroma[3][NINC] = { |
31 |
< |
{ /* X */ |
32 |
< |
0, 0, 0, 2, 6, 13, 22, 30, 36, 41, |
33 |
< |
42, 43, 43, 44, 46, 52, 60, 71, 87, 106, |
34 |
< |
128, 153, 178, 200, 219, 233, 243, 249, 252, 254, |
35 |
< |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
36 |
< |
}, { /* Y */ |
37 |
< |
0, 0, 0, 0, 0, 1, 2, 4, 7, 11, |
38 |
< |
17, 24, 34, 48, 64, 84, 105, 127, 148, 169, |
39 |
< |
188, 205, 220, 232, 240, 246, 250, 253, 254, 255, |
40 |
< |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
41 |
< |
}, { /* Z */ |
42 |
< |
0, 0, 2, 10, 32, 66, 118, 153, 191, 220, |
43 |
< |
237, 246, 251, 253, 254, 255, 255, 255, 255, 255, |
44 |
< |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
45 |
< |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
30 |
> |
|
31 |
> |
int CNDX[4] = {0,1,2,3}; /* RGBE indices for SCOLOR, SCOLR */ |
32 |
> |
float WLPART[4] = {780,588,480,380}; /* RGB wavelength limits+partitions (nm) */ |
33 |
> |
|
34 |
> |
|
35 |
> |
int |
36 |
> |
setspectrsamp( /* assign spectral sampling, 1 if good, -1 if bad */ |
37 |
> |
int cn[4], /* input cn[3]=nsamps */ |
38 |
> |
float wlpt[4] /* input wlpt[0],wlpt[3]=extrema */ |
39 |
> |
) |
40 |
> |
{ |
41 |
> |
static const float PKWL[3] = {607, 553, 469}; |
42 |
> |
int i, j; |
43 |
> |
|
44 |
> |
if (cn[3] < 3) |
45 |
> |
return(-1); /* reject this */ |
46 |
> |
|
47 |
> |
if (wlpt[0] < wlpt[3]) { |
48 |
> |
float tf = wlpt[0]; |
49 |
> |
wlpt[0] = wlpt[3]; wlpt[3] = tf; |
50 |
|
} |
51 |
< |
}; |
51 |
> |
if (wlpt[0] - wlpt[3] < 50.f) |
52 |
> |
return(-1); /* also reject */ |
53 |
|
|
54 |
+ |
if (cn[3] > MAXCSAMP) |
55 |
+ |
cn[3] = MAXCSAMP; |
56 |
|
|
57 |
< |
spec_rgb(col, s, e) /* comput RGB color from spectral range */ |
58 |
< |
COLOR col; |
59 |
< |
int s, e; |
57 |
> |
if ((wlpt[3] >= PKWL[2]) | (wlpt[0] <= PKWL[0])) { |
58 |
> |
wlpt[1] = wlpt[0] + 0.333333f*(wlpt[3]-wlpt[0]); |
59 |
> |
wlpt[2] = wlpt[0] + 0.666667f*(wlpt[3]-wlpt[0]); |
60 |
> |
cn[0] = 0; cn[1] = cn[3]/3; cn[2] = cn[3]*2/3; |
61 |
> |
return(0); /* unhappy but non-fatal return value */ |
62 |
> |
} |
63 |
> |
wlpt[1] = 588.f; /* tuned for standard green channel */ |
64 |
> |
wlpt[2] = 480.f; |
65 |
> |
if (cn[3] == 3) { /* nothing to tune? */ |
66 |
> |
cn[0] = 0; cn[1] = 1; cn[2] = 2; |
67 |
> |
} else { /* else find nearest color indices */ |
68 |
> |
double curwl[3]; |
69 |
> |
memset(curwl, 0, sizeof(curwl)); |
70 |
> |
for (i = cn[3]; i--; ) { |
71 |
> |
const float cwl = (i+.5f)/cn[3]*(wlpt[3]-wlpt[0]) + wlpt[0]; |
72 |
> |
for (j = 3; j--; ) |
73 |
> |
if (fabs(cwl - PKWL[j]) < fabs(curwl[j] - PKWL[j])) { |
74 |
> |
curwl[j] = cwl; |
75 |
> |
cn[j] = i; |
76 |
> |
} |
77 |
> |
} |
78 |
> |
} |
79 |
> |
return(1); /* happy return value */ |
80 |
> |
} |
81 |
> |
|
82 |
> |
|
83 |
> |
void |
84 |
> |
setscolor( /* assign spectral color from RGB */ |
85 |
> |
SCOLOR scol, |
86 |
> |
double r, |
87 |
> |
double g, |
88 |
> |
double b |
89 |
> |
) |
90 |
|
{ |
91 |
< |
COLOR ciecolor; |
91 |
> |
const double step = (WLPART[3] - WLPART[0])/(double)NCSAMP; |
92 |
> |
double cwl = WLPART[0] + .5*step; |
93 |
> |
int i; |
94 |
|
|
95 |
< |
spec_cie(ciecolor, s, e); |
96 |
< |
cie_rgb(col, ciecolor); |
95 |
> |
for (i = 0; i < NCSAMP; i++) { |
96 |
> |
if (cwl >= WLPART[1]) |
97 |
> |
scol[i] = r; |
98 |
> |
else if (cwl >= WLPART[2]) |
99 |
> |
scol[i] = g; |
100 |
> |
else |
101 |
> |
scol[i] = b; |
102 |
> |
cwl += step; |
103 |
> |
} |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
< |
spec_cie(col, s, e) /* compute a color from a spectral range */ |
108 |
< |
COLOR col; /* returned color */ |
109 |
< |
int s, e; /* starting and ending wavelengths */ |
107 |
> |
void |
108 |
> |
scolor2color( /* assign RGB color from spectrum */ |
109 |
> |
COLOR col, |
110 |
> |
SCOLOR scol, /* uses average over bands */ |
111 |
> |
int ncs, |
112 |
> |
float wlpt[4] |
113 |
> |
) |
114 |
|
{ |
115 |
< |
register int i, d, r; |
116 |
< |
|
117 |
< |
s -= STARTWL; |
66 |
< |
if (s < 0) |
67 |
< |
s = 0; |
115 |
> |
const double step = (wlpt[3] - wlpt[0])/(double)ncs; |
116 |
> |
double cwl = wlpt[0] + .5*step; |
117 |
> |
int i, j=0, n=0; |
118 |
|
|
119 |
< |
e -= STARTWL; |
120 |
< |
if (e >= INCWL*(NINC - 1)) |
121 |
< |
e = INCWL*(NINC - 1) - 1; |
119 |
> |
setcolor(col, 0, 0, 0); |
120 |
> |
for (i = 0; i < ncs; i++) { |
121 |
> |
if (cwl < wlpt[j+1]) { |
122 |
> |
if (n > 1) col[j] /= (COLORV)n; |
123 |
> |
j++; |
124 |
> |
n = 0; |
125 |
> |
} |
126 |
> |
col[j] += scol[i]; |
127 |
> |
n++; |
128 |
> |
cwl += step; |
129 |
> |
} |
130 |
> |
if (n > 1) col[j] /= (COLORV)n; |
131 |
> |
} |
132 |
|
|
73 |
– |
d = e / INCWL; /* interpolate values */ |
74 |
– |
r = e % INCWL; |
75 |
– |
for (i = 0; i < 3; i++) |
76 |
– |
col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r; |
133 |
|
|
134 |
< |
d = s / INCWL; |
135 |
< |
r = s % INCWL; |
136 |
< |
for (i = 0; i < 3; i++) |
137 |
< |
col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r; |
134 |
> |
void |
135 |
> |
scolor2colr( /* assign RGBE from spectral color */ |
136 |
> |
COLR clr, |
137 |
> |
SCOLOR scol, /* uses average over bands */ |
138 |
> |
int ncs, |
139 |
> |
float wlpt[4] |
140 |
> |
) |
141 |
> |
{ |
142 |
> |
COLOR col; |
143 |
|
|
144 |
< |
col[RED] = (col[RED] + 0.5) / (256*INCWL); |
145 |
< |
col[GRN] = (col[GRN] + 0.5) / (256*INCWL); |
85 |
< |
col[BLU] = (col[BLU] + 0.5) / (256*INCWL); |
144 |
> |
scolor2color(col, scol, ncs, wlpt); |
145 |
> |
setcolr(clr, col[RED], col[GRN], col[BLU]); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
< |
cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ |
150 |
< |
register COLOR rgbcolor, ciecolor; |
149 |
> |
void |
150 |
> |
scolor2scolr( /* float spectrum to common exponent */ |
151 |
> |
SCOLR sclr, |
152 |
> |
SCOLOR scol, |
153 |
> |
int ncs |
154 |
> |
) |
155 |
|
{ |
156 |
< |
static float cmat[3][3] = { |
157 |
< |
1.73, -.48, -.26, |
94 |
< |
-.81, 1.65, -.02, |
95 |
< |
.08, -.17, 1.28, |
96 |
< |
}; |
97 |
< |
register int i; |
156 |
> |
int i = ncs; |
157 |
> |
COLORV p = scol[--i]; |
158 |
|
|
159 |
< |
for (i = 0; i < 3; i++) { |
160 |
< |
rgbcolor[i] = cmat[i][0]*ciecolor[0] + |
161 |
< |
cmat[i][1]*ciecolor[1] + |
162 |
< |
cmat[i][2]*ciecolor[2] ; |
163 |
< |
if (rgbcolor[i] < 0.0) |
164 |
< |
rgbcolor[i] = 0.0; |
159 |
> |
while (i) |
160 |
> |
if (scol[--i] > p) |
161 |
> |
p = scol[i]; |
162 |
> |
if (p <= 1e-32) { |
163 |
> |
memset(sclr, 0, ncs+1); |
164 |
> |
return; |
165 |
|
} |
166 |
+ |
p = frexp(p, &i) * 256.0 / p; |
167 |
+ |
sclr[ncs] = i + COLXS; |
168 |
+ |
for (i = ncs; i--; ) |
169 |
+ |
sclr[i] = (scol[i] > 0) * (int)(scol[i]*p); |
170 |
|
} |
107 |
– |
#endif |
171 |
|
|
172 |
|
|
173 |
< |
fputresolu(ord, xres, yres, fp) /* put x and y resolution */ |
174 |
< |
register int ord; |
175 |
< |
int xres, yres; |
176 |
< |
FILE *fp; |
173 |
> |
void |
174 |
> |
scolr2scolor( /* common exponent to float spectrum */ |
175 |
> |
SCOLOR scol, |
176 |
> |
SCOLR sclr, |
177 |
> |
int ncs |
178 |
> |
) |
179 |
|
{ |
180 |
< |
if (ord&YMAJOR) |
181 |
< |
fprintf(fp, "%cY %d %cX %d\n", |
182 |
< |
ord&YDECR ? '-' : '+', yres, |
183 |
< |
ord&XDECR ? '-' : '+', xres); |
184 |
< |
else |
185 |
< |
fprintf(fp, "%cX %d %cY %d\n", |
186 |
< |
ord&XDECR ? '-' : '+', xres, |
187 |
< |
ord&YDECR ? '-' : '+', yres); |
180 |
> |
double f; |
181 |
> |
int i; |
182 |
> |
|
183 |
> |
if (sclr[ncs] == 0) { |
184 |
> |
memset(scol, 0, sizeof(COLORV)*ncs); |
185 |
> |
return; |
186 |
> |
} |
187 |
> |
f = ldexp(1.0, (int)sclr[ncs]-(COLXS+8)); |
188 |
> |
|
189 |
> |
for (i = ncs; i--; ) |
190 |
> |
scol[i] = (sclr[i] + 0.5)*f; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
< |
fgetresolu(xrp, yrp, fp) /* get x and y resolution */ |
195 |
< |
int *xrp, *yrp; |
196 |
< |
FILE *fp; |
194 |
> |
double |
195 |
> |
scolor_mean( /* compute average for spectral color */ |
196 |
> |
SCOLOR scol |
197 |
> |
) |
198 |
|
{ |
199 |
< |
char buf[64], *xndx, *yndx; |
200 |
< |
register char *cp; |
132 |
< |
register int ord; |
199 |
> |
int i = NCSAMP; |
200 |
> |
double sum = 0; |
201 |
|
|
202 |
< |
if (fgets(buf, sizeof(buf), fp) == NULL) |
203 |
< |
return(-1); |
204 |
< |
xndx = yndx = NULL; |
205 |
< |
for (cp = buf+1; *cp; cp++) |
138 |
< |
if (*cp == 'X') |
139 |
< |
xndx = cp; |
140 |
< |
else if (*cp == 'Y') |
141 |
< |
yndx = cp; |
142 |
< |
if (xndx == NULL || yndx == NULL) |
143 |
< |
return(-1); |
144 |
< |
ord = 0; |
145 |
< |
if (xndx > yndx) ord |= YMAJOR; |
146 |
< |
if (xndx[-1] == '-') ord |= XDECR; |
147 |
< |
if (yndx[-1] == '-') ord |= YDECR; |
148 |
< |
if ((*xrp = atoi(xndx+1)) <= 0) |
149 |
< |
return(-1); |
150 |
< |
if ((*yrp = atoi(yndx+1)) <= 0) |
151 |
< |
return(-1); |
152 |
< |
return(ord); |
202 |
> |
while (i--) |
203 |
> |
sum += scol[i]; |
204 |
> |
|
205 |
> |
return sum/(double)NCSAMP; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
210 |
< |
register COLR *scanline; |
211 |
< |
int len; |
212 |
< |
register FILE *fp; |
209 |
> |
double |
210 |
> |
sintens( /* find maximum value from spectrum */ |
211 |
> |
SCOLOR scol |
212 |
> |
) |
213 |
|
{ |
214 |
< |
COLR lastcolr; |
215 |
< |
int rept; |
214 |
> |
int i = NCSAMP; |
215 |
> |
COLORV peak = scol[--i]; |
216 |
> |
|
217 |
> |
while (i) |
218 |
> |
if (scol[--i] > peak) |
219 |
> |
peak = scol[i]; |
220 |
> |
|
221 |
> |
return peak; |
222 |
> |
} |
223 |
> |
|
224 |
> |
|
225 |
> |
void |
226 |
> |
convertscolor( /* spectrum conversion, zero-fill ends */ |
227 |
> |
SCOLOR dst, /* destination spectrum */ |
228 |
> |
int dnc, /* destination # of spectral samples/intervals */ |
229 |
> |
double dwl0, /* starting destination wavelength (longer) */ |
230 |
> |
double dwl1, /* ending destination wavelength (shorter) */ |
231 |
> |
const COLORV src[], /* source spectrum array */ |
232 |
> |
int snc, |
233 |
> |
double swl0, /* long/short wavelengths may be reversed */ |
234 |
> |
double swl1 |
235 |
> |
) |
236 |
> |
{ |
237 |
> |
const int sdir = 1 - 2*(swl0 < swl1); |
238 |
> |
const double sstp = (swl1 - swl0)/(double)snc; |
239 |
> |
const double dstp = (dwl1 - dwl0)/(double)dnc; |
240 |
> |
const double rdstp = 1./dstp; |
241 |
> |
int si, ssi, di; |
242 |
> |
double wl; |
243 |
> |
|
244 |
> |
if ((dnc < 3) | (dwl0 <= dwl1) | (dst == src)) |
245 |
> |
return; /* invalid destination */ |
246 |
> |
|
247 |
> |
if (dnc == snc && (dwl0-swl0)*(dwl0-swl0) + (dwl1-swl1)*(dwl1-swl1) <= .5) { |
248 |
> |
memcpy(dst, src, sizeof(COLORV)*dnc); |
249 |
> |
return; /* same spectral sampling */ |
250 |
> |
} |
251 |
> |
memset(dst, 0, sizeof(COLORV)*dnc); |
252 |
> |
/* set starting positions */ |
253 |
> |
if ((sdir>0 ? swl0 : swl1) <= dwl0) { |
254 |
> |
if (sdir > 0) { |
255 |
> |
wl = swl0; |
256 |
> |
ssi = 0; |
257 |
> |
} else { |
258 |
> |
wl = swl1; |
259 |
> |
ssi = snc-1; |
260 |
> |
} |
261 |
> |
si = 0; |
262 |
> |
di = (wl - dwl0)*rdstp; |
263 |
> |
} else { |
264 |
> |
wl = dwl0; |
265 |
> |
if (sdir > 0) { |
266 |
> |
ssi = si = (wl - swl0)/sstp; |
267 |
> |
} else { |
268 |
> |
si = (wl - swl1)/sstp; |
269 |
> |
ssi = snc-1 - si; |
270 |
> |
} |
271 |
> |
di = 0; |
272 |
> |
} |
273 |
> |
swl0 += (sdir < 0)*sstp; |
274 |
> |
/* step through intervals */ |
275 |
> |
while ((si < snc) & (di < dnc)) { |
276 |
> |
double intvl; |
277 |
> |
if (swl0 + (ssi+sdir)*sstp < dwl0 + (di+1)*dstp) { |
278 |
> |
intvl = dwl0 + (di+1)*dstp - wl; |
279 |
> |
dst[di++] += src[ssi]*intvl*rdstp; |
280 |
> |
} else { |
281 |
> |
intvl = swl0 + (ssi+sdir)*sstp - wl; |
282 |
> |
dst[di] += src[ssi]*intvl*rdstp; |
283 |
> |
ssi += sdir; |
284 |
> |
si++; |
285 |
> |
} |
286 |
> |
wl += intvl; |
287 |
> |
} |
288 |
> |
} |
289 |
> |
|
290 |
> |
|
291 |
> |
void * |
292 |
> |
tempbuffer( /* get a temporary buffer */ |
293 |
> |
size_t len |
294 |
> |
) |
295 |
> |
{ |
296 |
> |
static void *tempbuf = NULL; |
297 |
> |
static size_t tempbuflen = 0; |
298 |
> |
|
299 |
> |
if (!len) { /* call to free */ |
300 |
> |
if (tempbuflen) { |
301 |
> |
free(tempbuf); |
302 |
> |
tempbuf = NULL; |
303 |
> |
tempbuflen = 0; |
304 |
> |
} |
305 |
> |
return(NULL); |
306 |
> |
} |
307 |
> |
if (len <= tempbuflen) /* big enough already? */ |
308 |
> |
return(tempbuf); |
309 |
> |
/* else free & reallocate */ |
310 |
> |
if (tempbuflen) |
311 |
> |
free(tempbuf); |
312 |
> |
tempbuf = malloc(len); |
313 |
> |
tempbuflen = len*(tempbuf != NULL); |
314 |
> |
return(tempbuf); |
315 |
> |
} |
316 |
> |
|
317 |
> |
|
318 |
> |
int |
319 |
> |
fwritecolrs( /* write out a colr scanline */ |
320 |
> |
COLR *scanline, |
321 |
> |
int len, |
322 |
> |
FILE *fp |
323 |
> |
) |
324 |
> |
{ |
325 |
> |
int i, j, beg, cnt = 1; |
326 |
> |
int c2; |
327 |
|
|
328 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
329 |
< |
lastcolr[EXP] = 0; |
330 |
< |
rept = 0; |
331 |
< |
|
332 |
< |
while (len > 0) { |
333 |
< |
if (scanline[0][EXP] == lastcolr[EXP] && |
334 |
< |
scanline[0][RED] == lastcolr[RED] && |
335 |
< |
scanline[0][GRN] == lastcolr[GRN] && |
336 |
< |
scanline[0][BLU] == lastcolr[BLU]) |
337 |
< |
rept++; |
338 |
< |
else { |
339 |
< |
while (rept) { /* write out count */ |
340 |
< |
putc(1, fp); |
341 |
< |
putc(1, fp); |
342 |
< |
putc(1, fp); |
343 |
< |
putc(rept & 255, fp); |
344 |
< |
rept >>= 8; |
328 |
> |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ |
329 |
> |
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
330 |
> |
/* put magic header */ |
331 |
> |
putc(2, fp); |
332 |
> |
putc(2, fp); |
333 |
> |
putc(len>>8, fp); |
334 |
> |
putc(len&0xff, fp); |
335 |
> |
/* put components seperately */ |
336 |
> |
for (i = 0; i < 4; i++) { |
337 |
> |
for (j = 0; j < len; j += cnt) { /* find next run */ |
338 |
> |
for (beg = j; beg < len; beg += cnt) { |
339 |
> |
for (cnt = 1; (cnt < 127) & (beg+cnt < len) && |
340 |
> |
scanline[beg+cnt][i] == scanline[beg][i]; cnt++) |
341 |
> |
; |
342 |
> |
if (cnt >= MINRUN) |
343 |
> |
break; /* long enough */ |
344 |
> |
} |
345 |
> |
if ((beg-j > 1) & (beg-j < MINRUN)) { |
346 |
> |
c2 = j+1; |
347 |
> |
while (scanline[c2++][i] == scanline[j][i]) |
348 |
> |
if (c2 == beg) { /* short run */ |
349 |
> |
putc(128+beg-j, fp); |
350 |
> |
putc(scanline[j][i], fp); |
351 |
> |
j = beg; |
352 |
> |
break; |
353 |
|
} |
182 |
– |
putc(scanline[0][RED], fp); /* new color */ |
183 |
– |
putc(scanline[0][GRN], fp); |
184 |
– |
putc(scanline[0][BLU], fp); |
185 |
– |
putc(scanline[0][EXP], fp); |
186 |
– |
copycolr(lastcolr, scanline[0]); |
187 |
– |
rept = 0; |
354 |
|
} |
355 |
< |
scanline++; |
356 |
< |
len--; |
355 |
> |
while (j < beg) { /* write out non-run */ |
356 |
> |
if ((c2 = beg-j) > 128) c2 = 128; |
357 |
> |
putc(c2, fp); |
358 |
> |
while (c2--) |
359 |
> |
putc(scanline[j++][i], fp); |
360 |
> |
} |
361 |
> |
if (cnt >= MINRUN) { /* write out run */ |
362 |
> |
putc(128+cnt, fp); |
363 |
> |
putc(scanline[beg][i], fp); |
364 |
> |
} else |
365 |
> |
cnt = 0; |
366 |
> |
} |
367 |
|
} |
192 |
– |
while (rept) { /* write out count */ |
193 |
– |
putc(1, fp); |
194 |
– |
putc(1, fp); |
195 |
– |
putc(1, fp); |
196 |
– |
putc(rept & 255, fp); |
197 |
– |
rept >>= 8; |
198 |
– |
} |
368 |
|
return(ferror(fp) ? -1 : 0); |
369 |
|
} |
370 |
|
|
371 |
< |
|
372 |
< |
freadcolrs(scanline, len, fp) /* read in a colr scanline */ |
373 |
< |
register COLR *scanline; |
374 |
< |
int len; |
375 |
< |
register FILE *fp; |
371 |
> |
/* |
372 |
> |
* An old-format scanline is either a stream of valid RGBE or XYZE real |
373 |
> |
* pixels or at least one real pixel followed by some number of |
374 |
> |
* invalid real pixels of the form (1,1,1,n), where n is a count. |
375 |
> |
* These can themselves be repeated to create a multibyte repeat |
376 |
> |
* count, with the least significant byte first (little-endian order.) |
377 |
> |
* Repeat counts are limited by the size of an int; if a repetition |
378 |
> |
* leads to an overrun, the rest of the the repetition will be |
379 |
> |
* silently ignored. |
380 |
> |
*/ |
381 |
> |
static int |
382 |
> |
oldreadcolrs( /* read in an old-style colr scanline */ |
383 |
> |
COLR *scanline, |
384 |
> |
int len, |
385 |
> |
FILE *fp |
386 |
> |
) |
387 |
|
{ |
388 |
< |
int rshift; |
389 |
< |
register int i; |
388 |
> |
int rshift = 0; |
389 |
> |
int i; |
390 |
|
|
211 |
– |
rshift = 0; |
212 |
– |
|
391 |
|
while (len > 0) { |
392 |
|
scanline[0][RED] = getc(fp); |
393 |
|
scanline[0][GRN] = getc(fp); |
394 |
|
scanline[0][BLU] = getc(fp); |
395 |
< |
scanline[0][EXP] = getc(fp); |
396 |
< |
if (feof(fp) || ferror(fp)) |
395 |
> |
scanline[0][EXP] = i = getc(fp); |
396 |
> |
if (i == EOF) |
397 |
|
return(-1); |
398 |
< |
if (scanline[0][RED] == 1 && |
399 |
< |
scanline[0][GRN] == 1 && |
400 |
< |
scanline[0][BLU] == 1) { |
401 |
< |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
398 |
> |
if (scanline[0][GRN] == 1 && |
399 |
> |
(scanline[0][RED] == 1) & |
400 |
> |
(scanline[0][BLU] == 1)) { |
401 |
> |
i = scanline[0][EXP] << rshift; |
402 |
> |
while (i--) { |
403 |
|
copycolr(scanline[0], scanline[-1]); |
404 |
+ |
if (--len <= 0) |
405 |
+ |
return(0); |
406 |
|
scanline++; |
226 |
– |
len--; |
407 |
|
} |
408 |
|
rshift += 8; |
409 |
|
} else { |
415 |
|
return(0); |
416 |
|
} |
417 |
|
|
418 |
+ |
/* |
419 |
+ |
* There are two scanline formats: old and new. The old format |
420 |
+ |
* compresses runs of RGBE or XYZE four-byte real pixels; the new |
421 |
+ |
* format breaks the pixels into R, G, B, and E lines (or XYZE lines) |
422 |
+ |
* which are individually run-length encoded. |
423 |
+ |
* |
424 |
+ |
* An old-format scanline always begins with a valid real pixel; at |
425 |
+ |
* least one of the RGB (or XYZ) values will have its high-order bit |
426 |
+ |
* set. A new-format scanline begins with four bytes which are not a |
427 |
+ |
* valid real pixel: (2, 2, lenhigh, lenlow) where lenhigh is always |
428 |
+ |
* less than 128 and hence never has a high-order bit set. |
429 |
+ |
* |
430 |
+ |
* A new-format scanline is broken into its RGBE or XYZE components. |
431 |
+ |
* Each is output and run-length encoded separately so that a scanline |
432 |
+ |
* is broken into four records. In turn, each record is organized |
433 |
+ |
* into chunks of up to 128 characters, which begin with a count byte. |
434 |
+ |
* If the count byte is greater than 128, the following data byte is |
435 |
+ |
* repeated (count-128) times. If not, the count byte is followed by |
436 |
+ |
* that many data bytes. |
437 |
+ |
*/ |
438 |
+ |
int |
439 |
+ |
freadcolrs( /* read in an encoded colr scanline */ |
440 |
+ |
COLR *scanline, |
441 |
+ |
int len, |
442 |
+ |
FILE *fp |
443 |
+ |
) |
444 |
+ |
{ |
445 |
+ |
int i, j; |
446 |
+ |
int code, val; |
447 |
+ |
/* determine scanline type */ |
448 |
+ |
if (len <= 0) |
449 |
+ |
return(0); |
450 |
+ |
if ((i = getc(fp)) == EOF) |
451 |
+ |
return(-1); |
452 |
+ |
scanline[0][RED] = i; |
453 |
+ |
scanline[0][GRN] = getc(fp); |
454 |
+ |
scanline[0][BLU] = getc(fp); |
455 |
+ |
if ((i = getc(fp)) == EOF) |
456 |
+ |
return(-1); |
457 |
+ |
if ((scanline[0][RED] != 2) | (scanline[0][GRN] != 2) | |
458 |
+ |
(scanline[0][BLU] & 0x80)) { |
459 |
+ |
scanline[0][EXP] = i; |
460 |
+ |
return(oldreadcolrs(scanline+1, len-1, fp)); |
461 |
+ |
} |
462 |
+ |
if ((scanline[0][BLU]<<8 | i) != len) |
463 |
+ |
return(-1); /* length mismatch! */ |
464 |
+ |
/* read each component */ |
465 |
+ |
for (i = 0; i < 4; i++) |
466 |
+ |
for (j = 0; j < len; ) { |
467 |
+ |
if ((code = getc(fp)) == EOF) |
468 |
+ |
return(-1); |
469 |
+ |
if (code > 128) { /* run */ |
470 |
+ |
code &= 127; |
471 |
+ |
if ((val = getc(fp)) == EOF) |
472 |
+ |
return -1; |
473 |
+ |
if (j + code > len) |
474 |
+ |
return -1; /* overrun */ |
475 |
+ |
while (code--) |
476 |
+ |
scanline[j++][i] = val; |
477 |
+ |
} else { /* non-run */ |
478 |
+ |
if (j + code > len) |
479 |
+ |
return -1; /* overrun */ |
480 |
+ |
while (code--) { |
481 |
+ |
if ((val = getc(fp)) == EOF) |
482 |
+ |
return -1; |
483 |
+ |
scanline[j++][i] = val; |
484 |
+ |
} |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
return(0); |
488 |
+ |
} |
489 |
|
|
490 |
< |
fwritescan(scanline, len, fp) /* write out a scanline */ |
491 |
< |
register COLOR *scanline; |
492 |
< |
int len; |
493 |
< |
register FILE *fp; |
490 |
> |
|
491 |
> |
int |
492 |
> |
fwritescan( /* write out a scanline */ |
493 |
> |
COLOR *scanline, |
494 |
> |
int len, |
495 |
> |
FILE *fp |
496 |
> |
) |
497 |
|
{ |
498 |
< |
COLR lastcolr, thiscolr; |
499 |
< |
int rept; |
500 |
< |
|
501 |
< |
lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1; |
502 |
< |
lastcolr[EXP] = 0; |
503 |
< |
rept = 0; |
504 |
< |
|
505 |
< |
while (len > 0) { |
506 |
< |
setcolr(thiscolr, scanline[0][RED], |
498 |
> |
COLR *clrscan; |
499 |
> |
int n; |
500 |
> |
COLR *sp; |
501 |
> |
/* get scanline buffer */ |
502 |
> |
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
503 |
> |
return(-1); |
504 |
> |
clrscan = sp; |
505 |
> |
/* convert scanline */ |
506 |
> |
n = len; |
507 |
> |
while (n-- > 0) { |
508 |
> |
setcolr(sp[0], scanline[0][RED], |
509 |
|
scanline[0][GRN], |
510 |
|
scanline[0][BLU]); |
255 |
– |
if (thiscolr[EXP] == lastcolr[EXP] && |
256 |
– |
thiscolr[RED] == lastcolr[RED] && |
257 |
– |
thiscolr[GRN] == lastcolr[GRN] && |
258 |
– |
thiscolr[BLU] == lastcolr[BLU]) |
259 |
– |
rept++; |
260 |
– |
else { |
261 |
– |
while (rept) { /* write out count */ |
262 |
– |
putc(1, fp); |
263 |
– |
putc(1, fp); |
264 |
– |
putc(1, fp); |
265 |
– |
putc(rept & 255, fp); |
266 |
– |
rept >>= 8; |
267 |
– |
} |
268 |
– |
putc(thiscolr[RED], fp); /* new color */ |
269 |
– |
putc(thiscolr[GRN], fp); |
270 |
– |
putc(thiscolr[BLU], fp); |
271 |
– |
putc(thiscolr[EXP], fp); |
272 |
– |
copycolr(lastcolr, thiscolr); |
273 |
– |
rept = 0; |
274 |
– |
} |
511 |
|
scanline++; |
512 |
< |
len--; |
512 |
> |
sp++; |
513 |
|
} |
514 |
< |
while (rept) { /* write out count */ |
279 |
< |
putc(1, fp); |
280 |
< |
putc(1, fp); |
281 |
< |
putc(1, fp); |
282 |
< |
putc(rept & 255, fp); |
283 |
< |
rept >>= 8; |
284 |
< |
} |
285 |
< |
return(ferror(fp) ? -1 : 0); |
514 |
> |
return(fwritecolrs(clrscan, len, fp)); |
515 |
|
} |
516 |
|
|
517 |
|
|
518 |
< |
freadscan(scanline, len, fp) /* read in a scanline */ |
519 |
< |
register COLOR *scanline; |
520 |
< |
int len; |
521 |
< |
register FILE *fp; |
518 |
> |
int |
519 |
> |
freadscan( /* read in a scanline */ |
520 |
> |
COLOR *scanline, |
521 |
> |
int len, |
522 |
> |
FILE *fp |
523 |
> |
) |
524 |
|
{ |
525 |
< |
COLR thiscolr; |
526 |
< |
int rshift; |
527 |
< |
register int i; |
528 |
< |
|
529 |
< |
rshift = 0; |
530 |
< |
|
531 |
< |
while (len > 0) { |
532 |
< |
thiscolr[RED] = getc(fp); |
533 |
< |
thiscolr[GRN] = getc(fp); |
534 |
< |
thiscolr[BLU] = getc(fp); |
535 |
< |
thiscolr[EXP] = getc(fp); |
536 |
< |
if (feof(fp) || ferror(fp)) |
537 |
< |
return(-1); |
538 |
< |
if (thiscolr[RED] == 1 && |
539 |
< |
thiscolr[GRN] == 1 && |
540 |
< |
thiscolr[BLU] == 1) { |
541 |
< |
for (i = thiscolr[EXP] << rshift; i > 0; i--) { |
311 |
< |
copycolor(scanline[0], scanline[-1]); |
312 |
< |
scanline++; |
313 |
< |
len--; |
314 |
< |
} |
315 |
< |
rshift += 8; |
316 |
< |
} else { |
317 |
< |
colr_color(scanline[0], thiscolr); |
318 |
< |
scanline++; |
319 |
< |
len--; |
320 |
< |
rshift = 0; |
321 |
< |
} |
525 |
> |
COLR *clrscan; |
526 |
> |
|
527 |
> |
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
528 |
> |
return(-1); |
529 |
> |
if (freadcolrs(clrscan, len, fp) < 0) |
530 |
> |
return(-1); |
531 |
> |
/* convert scanline */ |
532 |
> |
colr_color(scanline[0], clrscan[0]); |
533 |
> |
while (--len > 0) { |
534 |
> |
scanline++; clrscan++; |
535 |
> |
if (clrscan[0][GRN] == clrscan[-1][GRN] && |
536 |
> |
(clrscan[0][RED] == clrscan[-1][RED]) & |
537 |
> |
(clrscan[0][BLU] == clrscan[-1][BLU]) & |
538 |
> |
(clrscan[0][EXP] == clrscan[-1][EXP])) |
539 |
> |
copycolor(scanline[0], scanline[-1]); |
540 |
> |
else |
541 |
> |
colr_color(scanline[0], clrscan[0]); |
542 |
|
} |
543 |
|
return(0); |
544 |
|
} |
545 |
|
|
546 |
|
|
547 |
< |
setcolr(clr, r, g, b) /* assign a short color value */ |
548 |
< |
register COLR clr; |
549 |
< |
double r, g, b; |
547 |
> |
void |
548 |
> |
setcolr( /* assign a short color value */ |
549 |
> |
COLR clr, |
550 |
> |
double r, |
551 |
> |
double g, |
552 |
> |
double b |
553 |
> |
) |
554 |
|
{ |
331 |
– |
double frexp(); |
555 |
|
double d; |
556 |
|
int e; |
557 |
|
|
566 |
|
|
567 |
|
d = frexp(d, &e) * 256.0 / d; |
568 |
|
|
569 |
< |
clr[RED] = r * d; |
570 |
< |
clr[GRN] = g * d; |
571 |
< |
clr[BLU] = b * d; |
569 |
> |
clr[RED] = (r > 0) * (int)(r*d); |
570 |
> |
clr[GRN] = (g > 0) * (int)(g*d); |
571 |
> |
clr[BLU] = (b > 0) * (int)(b*d); |
572 |
|
clr[EXP] = e + COLXS; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
< |
colr_color(col, clr) /* convert short to float color */ |
577 |
< |
register COLOR col; |
578 |
< |
register COLR clr; |
576 |
> |
void |
577 |
> |
colr_color( /* convert short to float color */ |
578 |
> |
COLOR col, |
579 |
> |
COLR clr |
580 |
> |
) |
581 |
|
{ |
582 |
|
double f; |
583 |
|
|
584 |
< |
if (clr[EXP] == 0) |
584 |
> |
if (clr[EXP] == 0) { |
585 |
|
col[RED] = col[GRN] = col[BLU] = 0.0; |
586 |
< |
else { |
362 |
< |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); |
363 |
< |
col[RED] = (clr[RED] + 0.5)*f; |
364 |
< |
col[GRN] = (clr[GRN] + 0.5)*f; |
365 |
< |
col[BLU] = (clr[BLU] + 0.5)*f; |
586 |
> |
return; |
587 |
|
} |
588 |
+ |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); |
589 |
+ |
col[RED] = (clr[RED] + 0.5)*f; |
590 |
+ |
col[GRN] = (clr[GRN] + 0.5)*f; |
591 |
+ |
col[BLU] = (clr[BLU] + 0.5)*f; |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
|
int |
596 |
< |
colr_norm(clr, nclr) /* normalize a short color, return shift */ |
597 |
< |
COLR clr, nclr; |
596 |
> |
bigdiff( /* c1 delta c2 > md? */ |
597 |
> |
COLOR c1, |
598 |
> |
COLOR c2, |
599 |
> |
double md |
600 |
> |
) |
601 |
|
{ |
602 |
< |
register int c; |
375 |
< |
register int shift = clr[EXP]-COLXS; |
602 |
> |
int i; |
603 |
|
|
604 |
< |
if (shift > 0) { |
605 |
< |
if (shift >= 8) { |
606 |
< |
nclr[RED] = nclr[GRN] = nclr[BLU] = 255; |
607 |
< |
} else { |
608 |
< |
c = clr[RED] << shift; |
609 |
< |
nclr[RED] = c > 255 ? 255 : c; |
610 |
< |
c = clr[GRN] << shift; |
611 |
< |
nclr[GRN] = c > 255 ? 255 : c; |
612 |
< |
c = clr[BLU] << shift; |
613 |
< |
nclr[BLU] = c > 255 ? 255 : c; |
614 |
< |
} |
615 |
< |
} else if (shift < 0) { |
616 |
< |
if (shift <= -8) { |
617 |
< |
nclr[RED] = nclr[GRN] = nclr[BLU] = 0; |
618 |
< |
} else { |
619 |
< |
nclr[RED] = clr[RED] >> -shift; |
620 |
< |
nclr[GRN] = clr[GRN] >> -shift; |
621 |
< |
nclr[BLU] = clr[BLU] >> -shift; |
622 |
< |
} |
623 |
< |
} else { |
624 |
< |
nclr[RED] = clr[RED]; |
398 |
< |
nclr[GRN] = clr[GRN]; |
399 |
< |
nclr[BLU] = clr[BLU]; |
400 |
< |
} |
401 |
< |
nclr[EXP] = COLXS; |
402 |
< |
return(shift); |
604 |
> |
for (i = 0; i < 3; i++) |
605 |
> |
if ((colval(c1,i)-colval(c2,i) > md*colval(c2,i)) | |
606 |
> |
(colval(c2,i)-colval(c1,i) > md*colval(c1,i))) |
607 |
> |
return(1); |
608 |
> |
return(0); |
609 |
> |
} |
610 |
> |
|
611 |
> |
|
612 |
> |
int |
613 |
> |
sbigsdiff( /* sc1 delta sc2 > md? */ |
614 |
> |
SCOLOR c1, |
615 |
> |
SCOLOR c2, |
616 |
> |
double md |
617 |
> |
) |
618 |
> |
{ |
619 |
> |
int i = NCSAMP; |
620 |
> |
|
621 |
> |
while (i--) |
622 |
> |
if ((c1[i]-c2[i] > md*c2[i]) | (c2[i]-c1[i] > md*c1[i])) |
623 |
> |
return(1); |
624 |
> |
return(0); |
625 |
|
} |