1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: color.c,v 2.27 2023/11/15 18:02:52 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* color.c - routines for color calculations. |
6 |
* |
7 |
* Externals declared in color.h |
8 |
*/ |
9 |
|
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 |
|
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 |
if (wlpt[0] - wlpt[3] < 50.f) |
52 |
return(-1); /* also reject */ |
53 |
|
54 |
if (cn[3] > MAXCSAMP) |
55 |
cn[3] = MAXCSAMP; |
56 |
|
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 |
const double step = (WLPART[3] - WLPART[0])/(double)NCSAMP; |
92 |
double cwl = WLPART[0] + .5*step; |
93 |
int i; |
94 |
|
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 |
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 |
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 |
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 |
|
133 |
|
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 |
scolor2color(col, scol, ncs, wlpt); |
145 |
setcolr(clr, col[RED], col[GRN], col[BLU]); |
146 |
} |
147 |
|
148 |
|
149 |
void |
150 |
scolor2scolr( /* float spectrum to common exponent */ |
151 |
SCOLR sclr, |
152 |
SCOLOR scol, |
153 |
int ncs |
154 |
) |
155 |
{ |
156 |
int i = ncs; |
157 |
COLORV p = scol[--i]; |
158 |
|
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 |
} |
171 |
|
172 |
|
173 |
void |
174 |
scolr2scolor( /* common exponent to float spectrum */ |
175 |
SCOLOR scol, |
176 |
SCOLR sclr, |
177 |
int ncs |
178 |
) |
179 |
{ |
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 |
double |
195 |
scolor_mean( /* compute average for spectral color */ |
196 |
SCOLOR scol |
197 |
) |
198 |
{ |
199 |
int i = NCSAMP; |
200 |
double sum = 0; |
201 |
|
202 |
while (i--) |
203 |
sum += scol[i]; |
204 |
|
205 |
return sum/(double)NCSAMP; |
206 |
} |
207 |
|
208 |
|
209 |
double |
210 |
sintens( /* find maximum value from spectrum */ |
211 |
SCOLOR scol |
212 |
) |
213 |
{ |
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 |
si = (wl - (sdir>0 ? swl0 : swl1))/sstp; |
266 |
ssi = sdir > 0 ? si : snc-1 - si; |
267 |
di = 0; |
268 |
} |
269 |
swl0 += (sdir < 0)*sstp; |
270 |
/* step through intervals */ |
271 |
while ((si < snc) & (di < dnc)) { |
272 |
double intvl; |
273 |
if (swl0 + (ssi+sdir)*sstp < dwl0 + (di+1)*dstp) { |
274 |
intvl = dwl0 + (di+1)*dstp - wl; |
275 |
dst[di++] += src[ssi]*intvl*rdstp; |
276 |
} else { |
277 |
intvl = swl0 + (ssi+sdir)*sstp - wl; |
278 |
dst[di] += src[ssi]*intvl*rdstp; |
279 |
ssi += sdir; |
280 |
si++; |
281 |
} |
282 |
wl += intvl; |
283 |
} |
284 |
} |
285 |
|
286 |
|
287 |
void * |
288 |
tempbuffer( /* get a temporary buffer */ |
289 |
size_t len |
290 |
) |
291 |
{ |
292 |
static void *tempbuf = NULL; |
293 |
static size_t tempbuflen = 0; |
294 |
|
295 |
if (!len) { /* call to free */ |
296 |
if (tempbuflen) { |
297 |
free(tempbuf); |
298 |
tempbuf = NULL; |
299 |
tempbuflen = 0; |
300 |
} |
301 |
return(NULL); |
302 |
} |
303 |
if (len <= tempbuflen) /* big enough already? */ |
304 |
return(tempbuf); |
305 |
/* else free & reallocate */ |
306 |
if (tempbuflen) |
307 |
free(tempbuf); |
308 |
tempbuf = malloc(len); |
309 |
tempbuflen = len*(tempbuf != NULL); |
310 |
return(tempbuf); |
311 |
} |
312 |
|
313 |
|
314 |
int |
315 |
fwritecolrs( /* write out a colr scanline */ |
316 |
COLR *scanline, |
317 |
int len, |
318 |
FILE *fp |
319 |
) |
320 |
{ |
321 |
int i, j, beg, cnt = 1; |
322 |
int c2; |
323 |
|
324 |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */ |
325 |
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
326 |
/* put magic header */ |
327 |
putc(2, fp); |
328 |
putc(2, fp); |
329 |
putc(len>>8, fp); |
330 |
putc(len&0xff, fp); |
331 |
/* put components seperately */ |
332 |
for (i = 0; i < 4; i++) { |
333 |
for (j = 0; j < len; j += cnt) { /* find next run */ |
334 |
for (beg = j; beg < len; beg += cnt) { |
335 |
for (cnt = 1; (cnt < 127) & (beg+cnt < len) && |
336 |
scanline[beg+cnt][i] == scanline[beg][i]; cnt++) |
337 |
; |
338 |
if (cnt >= MINRUN) |
339 |
break; /* long enough */ |
340 |
} |
341 |
if ((beg-j > 1) & (beg-j < MINRUN)) { |
342 |
c2 = j+1; |
343 |
while (scanline[c2++][i] == scanline[j][i]) |
344 |
if (c2 == beg) { /* short run */ |
345 |
putc(128+beg-j, fp); |
346 |
putc(scanline[j][i], fp); |
347 |
j = beg; |
348 |
break; |
349 |
} |
350 |
} |
351 |
while (j < beg) { /* write out non-run */ |
352 |
if ((c2 = beg-j) > 128) c2 = 128; |
353 |
putc(c2, fp); |
354 |
while (c2--) |
355 |
putc(scanline[j++][i], fp); |
356 |
} |
357 |
if (cnt >= MINRUN) { /* write out run */ |
358 |
putc(128+cnt, fp); |
359 |
putc(scanline[beg][i], fp); |
360 |
} else |
361 |
cnt = 0; |
362 |
} |
363 |
} |
364 |
return(ferror(fp) ? -1 : 0); |
365 |
} |
366 |
|
367 |
/* |
368 |
* An old-format scanline is either a stream of valid RGBE or XYZE real |
369 |
* pixels or at least one real pixel followed by some number of |
370 |
* invalid real pixels of the form (1,1,1,n), where n is a count. |
371 |
* These can themselves be repeated to create a multibyte repeat |
372 |
* count, with the least significant byte first (little-endian order.) |
373 |
* Repeat counts are limited by the size of an int; if a repetition |
374 |
* leads to an overrun, the rest of the the repetition will be |
375 |
* silently ignored. |
376 |
*/ |
377 |
static int |
378 |
oldreadcolrs( /* read in an old-style colr scanline */ |
379 |
COLR *scanline, |
380 |
int len, |
381 |
FILE *fp |
382 |
) |
383 |
{ |
384 |
int rshift = 0; |
385 |
int i; |
386 |
|
387 |
while (len > 0) { |
388 |
scanline[0][RED] = getc(fp); |
389 |
scanline[0][GRN] = getc(fp); |
390 |
scanline[0][BLU] = getc(fp); |
391 |
scanline[0][EXP] = i = getc(fp); |
392 |
if (i == EOF) |
393 |
return(-1); |
394 |
if (scanline[0][GRN] == 1 && |
395 |
(scanline[0][RED] == 1) & |
396 |
(scanline[0][BLU] == 1)) { |
397 |
i = scanline[0][EXP] << rshift; |
398 |
while (i--) { |
399 |
copycolr(scanline[0], scanline[-1]); |
400 |
if (--len <= 0) |
401 |
return(0); |
402 |
scanline++; |
403 |
} |
404 |
rshift += 8; |
405 |
} else { |
406 |
scanline++; |
407 |
len--; |
408 |
rshift = 0; |
409 |
} |
410 |
} |
411 |
return(0); |
412 |
} |
413 |
|
414 |
/* |
415 |
* There are two scanline formats: old and new. The old format |
416 |
* compresses runs of RGBE or XYZE four-byte real pixels; the new |
417 |
* format breaks the pixels into R, G, B, and E lines (or XYZE lines) |
418 |
* which are individually run-length encoded. |
419 |
* |
420 |
* An old-format scanline always begins with a valid real pixel; at |
421 |
* least one of the RGB (or XYZ) values will have its high-order bit |
422 |
* set. A new-format scanline begins with four bytes which are not a |
423 |
* valid real pixel: (2, 2, lenhigh, lenlow) where lenhigh is always |
424 |
* less than 128 and hence never has a high-order bit set. |
425 |
* |
426 |
* A new-format scanline is broken into its RGBE or XYZE components. |
427 |
* Each is output and run-length encoded separately so that a scanline |
428 |
* is broken into four records. In turn, each record is organized |
429 |
* into chunks of up to 128 characters, which begin with a count byte. |
430 |
* If the count byte is greater than 128, the following data byte is |
431 |
* repeated (count-128) times. If not, the count byte is followed by |
432 |
* that many data bytes. |
433 |
*/ |
434 |
int |
435 |
freadcolrs( /* read in an encoded colr scanline */ |
436 |
COLR *scanline, |
437 |
int len, |
438 |
FILE *fp |
439 |
) |
440 |
{ |
441 |
int i, j; |
442 |
int code, val; |
443 |
/* determine scanline type */ |
444 |
if (len <= 0) |
445 |
return(0); |
446 |
if ((i = getc(fp)) == EOF) |
447 |
return(-1); |
448 |
scanline[0][RED] = i; |
449 |
scanline[0][GRN] = getc(fp); |
450 |
scanline[0][BLU] = getc(fp); |
451 |
if ((i = getc(fp)) == EOF) |
452 |
return(-1); |
453 |
if ((scanline[0][RED] != 2) | (scanline[0][GRN] != 2) | |
454 |
(scanline[0][BLU] & 0x80)) { |
455 |
scanline[0][EXP] = i; |
456 |
return(oldreadcolrs(scanline+1, len-1, fp)); |
457 |
} |
458 |
if ((scanline[0][BLU]<<8 | i) != len) |
459 |
return(-1); /* length mismatch! */ |
460 |
/* read each component */ |
461 |
for (i = 0; i < 4; i++) |
462 |
for (j = 0; j < len; ) { |
463 |
if ((code = getc(fp)) == EOF) |
464 |
return(-1); |
465 |
if (code > 128) { /* run */ |
466 |
code &= 127; |
467 |
if ((val = getc(fp)) == EOF) |
468 |
return -1; |
469 |
if (j + code > len) |
470 |
return -1; /* overrun */ |
471 |
while (code--) |
472 |
scanline[j++][i] = val; |
473 |
} else { /* non-run */ |
474 |
if (j + code > len) |
475 |
return -1; /* overrun */ |
476 |
while (code--) { |
477 |
if ((val = getc(fp)) == EOF) |
478 |
return -1; |
479 |
scanline[j++][i] = val; |
480 |
} |
481 |
} |
482 |
} |
483 |
return(0); |
484 |
} |
485 |
|
486 |
|
487 |
int |
488 |
fwritescan( /* write out a scanline */ |
489 |
COLOR *scanline, |
490 |
int len, |
491 |
FILE *fp |
492 |
) |
493 |
{ |
494 |
COLR *clrscan; |
495 |
int n; |
496 |
COLR *sp; |
497 |
/* get scanline buffer */ |
498 |
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
499 |
return(-1); |
500 |
clrscan = sp; |
501 |
/* convert scanline */ |
502 |
n = len; |
503 |
while (n-- > 0) { |
504 |
setcolr(sp[0], scanline[0][RED], |
505 |
scanline[0][GRN], |
506 |
scanline[0][BLU]); |
507 |
scanline++; |
508 |
sp++; |
509 |
} |
510 |
return(fwritecolrs(clrscan, len, fp)); |
511 |
} |
512 |
|
513 |
|
514 |
int |
515 |
freadscan( /* read in a scanline */ |
516 |
COLOR *scanline, |
517 |
int len, |
518 |
FILE *fp |
519 |
) |
520 |
{ |
521 |
COLR *clrscan; |
522 |
|
523 |
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) |
524 |
return(-1); |
525 |
if (freadcolrs(clrscan, len, fp) < 0) |
526 |
return(-1); |
527 |
/* convert scanline */ |
528 |
colr_color(scanline[0], clrscan[0]); |
529 |
while (--len > 0) { |
530 |
scanline++; clrscan++; |
531 |
if (clrscan[0][GRN] == clrscan[-1][GRN] && |
532 |
(clrscan[0][RED] == clrscan[-1][RED]) & |
533 |
(clrscan[0][BLU] == clrscan[-1][BLU]) & |
534 |
(clrscan[0][EXP] == clrscan[-1][EXP])) |
535 |
copycolor(scanline[0], scanline[-1]); |
536 |
else |
537 |
colr_color(scanline[0], clrscan[0]); |
538 |
} |
539 |
return(0); |
540 |
} |
541 |
|
542 |
|
543 |
void |
544 |
setcolr( /* assign a short color value */ |
545 |
COLR clr, |
546 |
double r, |
547 |
double g, |
548 |
double b |
549 |
) |
550 |
{ |
551 |
double d; |
552 |
int e; |
553 |
|
554 |
d = r > g ? r : g; |
555 |
if (b > d) d = b; |
556 |
|
557 |
if (d <= 1e-32) { |
558 |
clr[RED] = clr[GRN] = clr[BLU] = 0; |
559 |
clr[EXP] = 0; |
560 |
return; |
561 |
} |
562 |
|
563 |
d = frexp(d, &e) * 256.0 / d; |
564 |
|
565 |
clr[RED] = (r > 0) * (int)(r*d); |
566 |
clr[GRN] = (g > 0) * (int)(g*d); |
567 |
clr[BLU] = (b > 0) * (int)(b*d); |
568 |
clr[EXP] = e + COLXS; |
569 |
} |
570 |
|
571 |
|
572 |
void |
573 |
colr_color( /* convert short to float color */ |
574 |
COLOR col, |
575 |
COLR clr |
576 |
) |
577 |
{ |
578 |
double f; |
579 |
|
580 |
if (clr[EXP] == 0) { |
581 |
col[RED] = col[GRN] = col[BLU] = 0.0; |
582 |
return; |
583 |
} |
584 |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8)); |
585 |
col[RED] = (clr[RED] + 0.5)*f; |
586 |
col[GRN] = (clr[GRN] + 0.5)*f; |
587 |
col[BLU] = (clr[BLU] + 0.5)*f; |
588 |
} |
589 |
|
590 |
|
591 |
int |
592 |
bigdiff( /* c1 delta c2 > md? */ |
593 |
COLOR c1, |
594 |
COLOR c2, |
595 |
double md |
596 |
) |
597 |
{ |
598 |
int i; |
599 |
|
600 |
for (i = 0; i < 3; i++) |
601 |
if ((colval(c1,i)-colval(c2,i) > md*colval(c2,i)) | |
602 |
(colval(c2,i)-colval(c1,i) > md*colval(c1,i))) |
603 |
return(1); |
604 |
return(0); |
605 |
} |
606 |
|
607 |
|
608 |
int |
609 |
sbigsdiff( /* sc1 delta sc2 > md? */ |
610 |
SCOLOR c1, |
611 |
SCOLOR c2, |
612 |
double md |
613 |
) |
614 |
{ |
615 |
int i = NCSAMP; |
616 |
|
617 |
while (i--) |
618 |
if ((c1[i]-c2[i] > md*c2[i]) | (c2[i]-c1[i] > md*c1[i])) |
619 |
return(1); |
620 |
return(0); |
621 |
} |