ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/color.c
Revision: 2.38
Committed: Fri Sep 20 17:39:12 2024 UTC (7 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.37: +16 -1 lines
Log Message:
feat(pextrem): Added handling of hyperspectral pictures

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.38 static const char RCSid[] = "$Id: color.c,v 2.37 2024/09/11 01:34:40 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * color.c - routines for color calculations.
6     *
7 greg 2.9 * Externals declared in color.h
8     */
9    
10 greg 2.10 #include "copyright.h"
11 greg 1.1
12     #include <stdio.h>
13 greg 2.9 #include <stdlib.h>
14 greg 2.7 #include <math.h>
15 greg 1.1 #include "color.h"
16 greg 2.14
17     #ifdef getc_unlocked /* avoid horrendous overhead of flockfile */
18 greg 2.15 #undef getc
19     #undef putc
20 greg 2.19 #undef ferror
21 greg 2.14 #define getc getc_unlocked
22     #define putc putc_unlocked
23 greg 2.18 #define ferror ferror_unlocked
24 greg 2.14 #endif
25 greg 1.1
26 greg 2.34 #define MINELEN 17 /* minimum scanline length for encoding */
27 greg 2.6 #define MAXELEN 0x7fff /* maximum scanline length for encoding */
28 greg 1.14 #define MINRUN 4 /* minimum run length */
29    
30 greg 2.4
31 greg 2.27 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 greg 2.38 setscolr( /* assign common-exponent spectral from RGB */
109     SCOLR sclr,
110     double r,
111     double g,
112     double b
113     )
114     {
115     SCOLOR scol;
116    
117     setscolor(scol, r, g, b);
118     scolor2scolr(sclr, scol, NCSAMP);
119     }
120    
121    
122     void
123 greg 2.27 scolor2color( /* assign RGB color from spectrum */
124     COLOR col,
125 greg 2.35 const SCOLOR scol, /* uses average over bands */
126 greg 2.27 int ncs,
127 greg 2.30 const float wlpt[4]
128 greg 2.27 )
129     {
130     const double step = (wlpt[3] - wlpt[0])/(double)ncs;
131     double cwl = wlpt[0] + .5*step;
132     int i, j=0, n=0;
133    
134     setcolor(col, 0, 0, 0);
135     for (i = 0; i < ncs; i++) {
136     if (cwl < wlpt[j+1]) {
137     if (n > 1) col[j] /= (COLORV)n;
138     j++;
139     n = 0;
140     }
141     col[j] += scol[i];
142     n++;
143     cwl += step;
144     }
145     if (n > 1) col[j] /= (COLORV)n;
146     }
147    
148    
149     void
150 greg 2.36 scolr2colr( /* assign RGBE from common-exponent spectrum */
151     COLR clr,
152     const SCOLR sclr,
153     int ncs,
154     const float wlpt[4]
155     )
156 greg 2.37 #if 0 /* fancier method seems to be slower(!) */
157 greg 2.36 {
158     const double step = (wlpt[3] - wlpt[0])/(double)ncs;
159     double cwl;
160     int csum[3], cnt[3], eshft;
161     int i, j;
162    
163     csum[0] = csum[1] = csum[2] = 0;
164     cnt[0] = cnt[1] = cnt[2] = 0;
165     cwl = wlpt[j=0] + .5*step;
166     for (i = 0; i < ncs; i++) {
167     csum[j] += sclr[i];
168     ++cnt[j];
169     j += ((cwl += step) < wlpt[j+1]);
170     }
171     eshft = 7; /* compute exponent shift */
172     for (j = 3; (eshft > 0) & (j-- > 0); ) {
173     i = 0;
174     while (csum[j] < 128*cnt[j] >> i)
175     if (++i >= eshft)
176     break;
177     if (eshft > i)
178     eshft = i;
179     }
180     if (sclr[ncs] <= eshft) {
181     clr[RED] = clr[GRN] = clr[BLU] = 0;
182     clr[EXP] = 0;
183     return;
184     }
185     for (j = 3; j--; )
186     clr[j] = (csum[j]<<eshft)/cnt[j];
187    
188     clr[EXP] = sclr[ncs] - eshft;
189     }
190 greg 2.37 #else
191     {
192     SCOLOR scol;
193     COLOR col;
194    
195     scolr2scolor(scol, sclr, ncs);
196     scolor2color(col, scol, ncs, wlpt);
197     setcolr(clr, col[RED], col[GRN], col[BLU]);
198     }
199     #endif
200 greg 2.36
201    
202     void
203 greg 2.27 scolor2colr( /* assign RGBE from spectral color */
204     COLR clr,
205 greg 2.35 const SCOLOR scol, /* uses average over bands */
206 greg 2.27 int ncs,
207 greg 2.30 const float wlpt[4]
208 greg 2.27 )
209     {
210     COLOR col;
211    
212     scolor2color(col, scol, ncs, wlpt);
213     setcolr(clr, col[RED], col[GRN], col[BLU]);
214     }
215    
216    
217     void
218 greg 2.33 scolr2color( /* assign RGB from common exponent */
219     COLOR col,
220 greg 2.35 const SCOLR sclr,
221 greg 2.33 int ncs,
222     const float wlpt[4]
223     )
224     {
225     SCOLOR scol;
226    
227     scolr2scolor(scol, sclr, ncs);
228     scolor2color(col, scol, ncs, wlpt);
229     }
230    
231    
232     void
233 greg 2.27 scolor2scolr( /* float spectrum to common exponent */
234     SCOLR sclr,
235 greg 2.35 const SCOLOR scol,
236 greg 2.27 int ncs
237     )
238     {
239     int i = ncs;
240     COLORV p = scol[--i];
241    
242     while (i)
243     if (scol[--i] > p)
244     p = scol[i];
245     if (p <= 1e-32) {
246     memset(sclr, 0, ncs+1);
247     return;
248     }
249     p = frexp(p, &i) * 256.0 / p;
250     sclr[ncs] = i + COLXS;
251     for (i = ncs; i--; )
252     sclr[i] = (scol[i] > 0) * (int)(scol[i]*p);
253     }
254    
255    
256     void
257     scolr2scolor( /* common exponent to float spectrum */
258     SCOLOR scol,
259 greg 2.35 const SCOLR sclr,
260 greg 2.27 int ncs
261     )
262     {
263     double f;
264     int i;
265    
266     if (sclr[ncs] == 0) {
267     memset(scol, 0, sizeof(COLORV)*ncs);
268     return;
269     }
270     f = ldexp(1.0, (int)sclr[ncs]-(COLXS+8));
271    
272     for (i = ncs; i--; )
273     scol[i] = (sclr[i] + 0.5)*f;
274     }
275    
276    
277     double
278     scolor_mean( /* compute average for spectral color */
279 greg 2.35 const SCOLOR scol
280 greg 2.27 )
281     {
282     int i = NCSAMP;
283     double sum = 0;
284    
285     while (i--)
286     sum += scol[i];
287    
288     return sum/(double)NCSAMP;
289     }
290    
291    
292     double
293     sintens( /* find maximum value from spectrum */
294 greg 2.35 const SCOLOR scol
295 greg 2.27 )
296     {
297     int i = NCSAMP;
298     COLORV peak = scol[--i];
299    
300     while (i)
301     if (scol[--i] > peak)
302     peak = scol[i];
303    
304     return peak;
305     }
306    
307    
308     void
309     convertscolor( /* spectrum conversion, zero-fill ends */
310     SCOLOR dst, /* destination spectrum */
311     int dnc, /* destination # of spectral samples/intervals */
312     double dwl0, /* starting destination wavelength (longer) */
313     double dwl1, /* ending destination wavelength (shorter) */
314     const COLORV src[], /* source spectrum array */
315     int snc,
316     double swl0, /* long/short wavelengths may be reversed */
317     double swl1
318     )
319     {
320     const int sdir = 1 - 2*(swl0 < swl1);
321     const double sstp = (swl1 - swl0)/(double)snc;
322     const double dstp = (dwl1 - dwl0)/(double)dnc;
323     const double rdstp = 1./dstp;
324     int si, ssi, di;
325     double wl;
326    
327     if ((dnc < 3) | (dwl0 <= dwl1) | (dst == src))
328     return; /* invalid destination */
329    
330     if (dnc == snc && (dwl0-swl0)*(dwl0-swl0) + (dwl1-swl1)*(dwl1-swl1) <= .5) {
331     memcpy(dst, src, sizeof(COLORV)*dnc);
332     return; /* same spectral sampling */
333     }
334     memset(dst, 0, sizeof(COLORV)*dnc);
335     /* set starting positions */
336     if ((sdir>0 ? swl0 : swl1) <= dwl0) {
337     if (sdir > 0) {
338     wl = swl0;
339     ssi = 0;
340     } else {
341     wl = swl1;
342     ssi = snc-1;
343     }
344     si = 0;
345     di = (wl - dwl0)*rdstp;
346     } else {
347     wl = dwl0;
348 greg 2.29 if (sdir > 0) {
349     ssi = si = (wl - swl0)/sstp;
350     } else {
351     si = (wl - swl1)/sstp;
352     ssi = snc-1 - si;
353     }
354 greg 2.27 di = 0;
355     }
356     swl0 += (sdir < 0)*sstp;
357     /* step through intervals */
358     while ((si < snc) & (di < dnc)) {
359     double intvl;
360     if (swl0 + (ssi+sdir)*sstp < dwl0 + (di+1)*dstp) {
361     intvl = dwl0 + (di+1)*dstp - wl;
362     dst[di++] += src[ssi]*intvl*rdstp;
363     } else {
364     intvl = swl0 + (ssi+sdir)*sstp - wl;
365     dst[di] += src[ssi]*intvl*rdstp;
366     ssi += sdir;
367     si++;
368     }
369     wl += intvl;
370     }
371     }
372    
373    
374 greg 2.22 void *
375 greg 2.17 tempbuffer( /* get a temporary buffer */
376 greg 2.27 size_t len
377 greg 2.17 )
378 greg 1.14 {
379 greg 2.27 static void *tempbuf = NULL;
380     static size_t tempbuflen = 0;
381 greg 1.14
382 greg 2.22 if (!len) { /* call to free */
383     if (tempbuflen) {
384 greg 2.20 free(tempbuf);
385 greg 2.22 tempbuf = NULL;
386     tempbuflen = 0;
387     }
388     return(NULL);
389 greg 1.14 }
390 greg 2.22 if (len <= tempbuflen) /* big enough already? */
391     return(tempbuf);
392     /* else free & reallocate */
393     if (tempbuflen)
394     free(tempbuf);
395     tempbuf = malloc(len);
396     tempbuflen = len*(tempbuf != NULL);
397 greg 1.14 return(tempbuf);
398     }
399    
400    
401 greg 2.9 int
402 greg 2.17 fwritecolrs( /* write out a colr scanline */
403     COLR *scanline,
404     int len,
405     FILE *fp
406     )
407 greg 1.1 {
408 greg 2.17 int i, j, beg, cnt = 1;
409 greg 1.14 int c2;
410 greg 1.1
411 schorsch 2.13 if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */
412 greg 1.14 return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
413 greg 2.2 /* put magic header */
414     putc(2, fp);
415 greg 1.14 putc(2, fp);
416     putc(len>>8, fp);
417 greg 2.21 putc(len&0xff, fp);
418 greg 1.14 /* put components seperately */
419     for (i = 0; i < 4; i++) {
420     for (j = 0; j < len; j += cnt) { /* find next run */
421     for (beg = j; beg < len; beg += cnt) {
422 greg 2.20 for (cnt = 1; (cnt < 127) & (beg+cnt < len) &&
423 greg 1.14 scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
424     ;
425     if (cnt >= MINRUN)
426     break; /* long enough */
427 greg 1.1 }
428 greg 2.20 if ((beg-j > 1) & (beg-j < MINRUN)) {
429 greg 1.15 c2 = j+1;
430     while (scanline[c2++][i] == scanline[j][i])
431     if (c2 == beg) { /* short run */
432     putc(128+beg-j, fp);
433     putc(scanline[j][i], fp);
434     j = beg;
435     break;
436     }
437     }
438     while (j < beg) { /* write out non-run */
439 greg 1.14 if ((c2 = beg-j) > 128) c2 = 128;
440     putc(c2, fp);
441     while (c2--)
442     putc(scanline[j++][i], fp);
443     }
444     if (cnt >= MINRUN) { /* write out run */
445     putc(128+cnt, fp);
446     putc(scanline[beg][i], fp);
447     } else
448     cnt = 0;
449     }
450 greg 1.1 }
451     return(ferror(fp) ? -1 : 0);
452     }
453    
454 greg 2.23 /*
455     * An old-format scanline is either a stream of valid RGBE or XYZE real
456     * pixels or at least one real pixel followed by some number of
457     * invalid real pixels of the form (1,1,1,n), where n is a count.
458     * These can themselves be repeated to create a multibyte repeat
459     * count, with the least significant byte first (little-endian order.)
460     * Repeat counts are limited by the size of an int; if a repetition
461     * leads to an overrun, the rest of the the repetition will be
462     * silently ignored.
463     */
464 greg 2.9 static int
465 greg 2.19 oldreadcolrs( /* read in an old-style colr scanline */
466 greg 2.17 COLR *scanline,
467     int len,
468     FILE *fp
469     )
470 greg 2.9 {
471 greg 2.25 int rshift = 0;
472 greg 2.17 int i;
473 greg 2.9
474     while (len > 0) {
475     scanline[0][RED] = getc(fp);
476     scanline[0][GRN] = getc(fp);
477     scanline[0][BLU] = getc(fp);
478 greg 2.18 scanline[0][EXP] = i = getc(fp);
479     if (i == EOF)
480 greg 2.9 return(-1);
481 greg 2.20 if (scanline[0][GRN] == 1 &&
482     (scanline[0][RED] == 1) &
483 greg 2.25 (scanline[0][BLU] == 1)) {
484 greg 2.20 i = scanline[0][EXP] << rshift;
485     while (i--) {
486 greg 2.9 copycolr(scanline[0], scanline[-1]);
487 greg 2.20 if (--len <= 0)
488     return(0);
489 greg 2.9 scanline++;
490     }
491     rshift += 8;
492     } else {
493     scanline++;
494     len--;
495     rshift = 0;
496     }
497     }
498     return(0);
499     }
500    
501 greg 2.23 /*
502     * There are two scanline formats: old and new. The old format
503     * compresses runs of RGBE or XYZE four-byte real pixels; the new
504     * format breaks the pixels into R, G, B, and E lines (or XYZE lines)
505     * which are individually run-length encoded.
506     *
507     * An old-format scanline always begins with a valid real pixel; at
508     * least one of the RGB (or XYZ) values will have its high-order bit
509     * set. A new-format scanline begins with four bytes which are not a
510     * valid real pixel: (2, 2, lenhigh, lenlow) where lenhigh is always
511     * less than 128 and hence never has a high-order bit set.
512     *
513     * A new-format scanline is broken into its RGBE or XYZE components.
514     * Each is output and run-length encoded separately so that a scanline
515     * is broken into four records. In turn, each record is organized
516     * into chunks of up to 128 characters, which begin with a count byte.
517     * If the count byte is greater than 128, the following data byte is
518     * repeated (count-128) times. If not, the count byte is followed by
519     * that many data bytes.
520     */
521 greg 2.9 int
522 greg 2.17 freadcolrs( /* read in an encoded colr scanline */
523     COLR *scanline,
524     int len,
525     FILE *fp
526     )
527 greg 1.1 {
528 greg 2.17 int i, j;
529 greg 2.6 int code, val;
530 greg 1.14 /* determine scanline type */
531 greg 2.26 if (len <= 0)
532     return(0);
533 greg 1.14 if ((i = getc(fp)) == EOF)
534     return(-1);
535 greg 2.26 scanline[0][RED] = i;
536 greg 1.14 scanline[0][GRN] = getc(fp);
537     scanline[0][BLU] = getc(fp);
538     if ((i = getc(fp)) == EOF)
539     return(-1);
540 greg 2.26 if ((scanline[0][RED] != 2) | (scanline[0][GRN] != 2) |
541     (scanline[0][BLU] & 0x80)) {
542 greg 1.14 scanline[0][EXP] = i;
543     return(oldreadcolrs(scanline+1, len-1, fp));
544     }
545     if ((scanline[0][BLU]<<8 | i) != len)
546     return(-1); /* length mismatch! */
547     /* read each component */
548     for (i = 0; i < 4; i++)
549     for (j = 0; j < len; ) {
550     if ((code = getc(fp)) == EOF)
551     return(-1);
552     if (code > 128) { /* run */
553 greg 2.6 code &= 127;
554 greg 2.9 if ((val = getc(fp)) == EOF)
555     return -1;
556 greg 2.16 if (j + code > len)
557     return -1; /* overrun */
558 greg 2.6 while (code--)
559     scanline[j++][i] = val;
560 greg 2.16 } else { /* non-run */
561     if (j + code > len)
562     return -1; /* overrun */
563 greg 2.9 while (code--) {
564     if ((val = getc(fp)) == EOF)
565     return -1;
566     scanline[j++][i] = val;
567     }
568 greg 2.16 }
569 greg 1.14 }
570 greg 1.1 return(0);
571     }
572    
573    
574 greg 2.30 /* read an nc-component common-exponent color scanline */
575     int
576 greg 2.35 freadscolrs(COLRV *scanline, int nc, int len, FILE *fp)
577 greg 2.30 {
578 greg 2.32 if (nc < 3)
579     return(-1);
580     if (nc == 3)
581     return(freadcolrs((COLR *)scanline, len, fp));
582    
583 greg 2.30 if (fread(scanline, nc+1, len, fp) != len)
584     return(-1);
585     return(0);
586     }
587    
588    
589 greg 2.36 /* read nc-component common-exponent color scan and convert to COLR's */
590     int
591     fread2colrs(COLR *scanline, int len, FILE *fp, int nc, const float wlpt[4])
592     {
593     COLRV *sclrscan;
594     int n;
595    
596     if (nc < 3)
597     return(-1);
598     if (nc == 3)
599     return(freadcolrs(scanline, len, fp));
600    
601     sclrscan = (COLRV *)tempbuffer(sizeof(COLRV)*(nc+1)*len);
602     if (sclrscan == NULL || freadscolrs(sclrscan, nc, len, fp) < 0)
603     return(-1);
604     for (n = len; n--; ) {
605     scolr2colr(*scanline++, sclrscan, nc, wlpt);
606     sclrscan += nc+1;
607     }
608     return(0);
609     }
610    
611    
612 greg 2.31 /* write an common-exponent spectral color scanline */
613 greg 2.30 int
614 greg 2.35 fwritescolrs(const COLRV *sscanline, int nc, int len, FILE *fp)
615 greg 2.30 {
616 greg 2.32 if (nc < 3)
617     return(-1);
618     if (nc == 3)
619     return(fwritecolrs((COLR *)sscanline, len, fp));
620    
621 greg 2.31 if (fwrite(sscanline, nc+1, len, fp) != len)
622 greg 2.30 return(-1);
623     return(0);
624     }
625    
626    
627 greg 2.9 int
628 greg 2.32 fwritescan( /* write out an RGB or XYZ scanline */
629 greg 2.17 COLOR *scanline,
630     int len,
631     FILE *fp
632     )
633 greg 1.1 {
634 greg 1.14 COLR *clrscan;
635     int n;
636 greg 2.17 COLR *sp;
637 greg 1.14 /* get scanline buffer */
638     if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
639     return(-1);
640     clrscan = sp;
641     /* convert scanline */
642     n = len;
643     while (n-- > 0) {
644     setcolr(sp[0], scanline[0][RED],
645 greg 1.1 scanline[0][GRN],
646     scanline[0][BLU]);
647     scanline++;
648 greg 1.14 sp++;
649 greg 1.1 }
650 greg 1.14 return(fwritecolrs(clrscan, len, fp));
651 greg 1.1 }
652    
653    
654 greg 2.9 int
655 greg 2.32 freadscan( /* read in an RGB or XYZ scanline */
656 greg 2.17 COLOR *scanline,
657     int len,
658     FILE *fp
659     )
660 greg 1.1 {
661 greg 2.17 COLR *clrscan;
662 greg 1.14
663     if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
664     return(-1);
665     if (freadcolrs(clrscan, len, fp) < 0)
666     return(-1);
667     /* convert scanline */
668     colr_color(scanline[0], clrscan[0]);
669     while (--len > 0) {
670     scanline++; clrscan++;
671 greg 2.20 if (clrscan[0][GRN] == clrscan[-1][GRN] &&
672     (clrscan[0][RED] == clrscan[-1][RED]) &
673     (clrscan[0][BLU] == clrscan[-1][BLU]) &
674     (clrscan[0][EXP] == clrscan[-1][EXP]))
675 greg 1.14 copycolor(scanline[0], scanline[-1]);
676     else
677     colr_color(scanline[0], clrscan[0]);
678 greg 1.1 }
679     return(0);
680     }
681    
682    
683 greg 2.30 /* read an nc-component color scanline */
684     int
685     freadsscan(COLORV *sscanline, int nc, int len, FILE *fp)
686     {
687 greg 2.35 COLRV *tscn = (COLRV *)tempbuffer((nc+1)*len);
688 greg 2.30 int i;
689    
690     if (tscn == NULL || freadscolrs(tscn, nc, len, fp) < 0)
691     return(-1);
692     for (i = len; i-- > 0; ) {
693     scolr2scolor(sscanline, tscn, nc);
694     sscanline += nc;
695     tscn += nc+1;
696     }
697     return(0);
698     }
699    
700    
701 greg 2.36 /* read an nc-component color scanline and return as RGB */
702     int
703     fread2scan(COLOR *scanline, int len, FILE *fp, int nc, const float wlpt[4])
704     {
705     COLRV *tscn;
706     int i;
707    
708     if (nc < 3)
709     return(-1);
710     if (nc == 3)
711     return(freadscan(scanline, len, fp));
712    
713     tscn = (COLRV *)tempbuffer((nc+1)*len);
714     if (tscn == NULL || freadscolrs(tscn, nc, len, fp) < 0)
715     return(-1);
716     for (i = len; i-- > 0; ) {
717     scolr2color(*scanline++, tscn, nc, wlpt);
718     tscn += nc+1;
719     }
720     return(0);
721     }
722    
723    
724 greg 2.32 /* write an nc-component spectral color scanline */
725 greg 2.30 int
726 greg 2.35 fwritesscan(const COLORV *sscanline, int nc, int len, FILE *fp)
727 greg 2.30 {
728 greg 2.35 COLRV *tscn = (COLRV *)tempbuffer((nc+1)*len);
729 greg 2.30 int i;
730    
731     if (tscn == NULL)
732     return(-1);
733     for (i = 0; i < len; i++) {
734 greg 2.31 scolor2scolr(tscn+i*(nc+1), sscanline, nc);
735     sscanline += nc;
736 greg 2.30 }
737 greg 2.31 return(fwritescolrs(tscn, nc, len, fp));
738 greg 2.30 }
739    
740    
741 greg 2.9 void
742 greg 2.17 setcolr( /* assign a short color value */
743     COLR clr,
744     double r,
745     double g,
746     double b
747     )
748 greg 1.1 {
749     double d;
750     int e;
751    
752     d = r > g ? r : g;
753     if (b > d) d = b;
754    
755 greg 1.4 if (d <= 1e-32) {
756 greg 1.1 clr[RED] = clr[GRN] = clr[BLU] = 0;
757     clr[EXP] = 0;
758     return;
759     }
760    
761 greg 2.21 d = frexp(d, &e) * 256.0 / d;
762 greg 1.1
763 greg 2.27 clr[RED] = (r > 0) * (int)(r*d);
764     clr[GRN] = (g > 0) * (int)(g*d);
765     clr[BLU] = (b > 0) * (int)(b*d);
766 greg 1.1 clr[EXP] = e + COLXS;
767     }
768    
769    
770 greg 2.9 void
771 greg 2.17 colr_color( /* convert short to float color */
772     COLOR col,
773 greg 2.35 const COLR clr
774 greg 2.17 )
775 greg 1.1 {
776 greg 1.6 double f;
777 greg 1.1
778 greg 2.27 if (clr[EXP] == 0) {
779 greg 1.1 col[RED] = col[GRN] = col[BLU] = 0.0;
780 greg 2.27 return;
781 greg 1.1 }
782 greg 2.27 f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
783     col[RED] = (clr[RED] + 0.5)*f;
784     col[GRN] = (clr[GRN] + 0.5)*f;
785     col[BLU] = (clr[BLU] + 0.5)*f;
786 greg 1.6 }
787    
788    
789 greg 2.9 int
790 greg 2.17 bigdiff( /* c1 delta c2 > md? */
791 greg 2.35 const COLOR c1,
792     const COLOR c2,
793 greg 2.17 double md
794     )
795 greg 1.7 {
796 greg 2.17 int i;
797 greg 1.7
798     for (i = 0; i < 3; i++)
799 greg 2.27 if ((colval(c1,i)-colval(c2,i) > md*colval(c2,i)) |
800     (colval(c2,i)-colval(c1,i) > md*colval(c1,i)))
801     return(1);
802     return(0);
803     }
804    
805    
806     int
807     sbigsdiff( /* sc1 delta sc2 > md? */
808 greg 2.35 const SCOLOR c1,
809     const SCOLOR c2,
810 greg 2.27 double md
811     )
812     {
813     int i = NCSAMP;
814    
815     while (i--)
816     if ((c1[i]-c2[i] > md*c2[i]) | (c2[i]-c1[i] > md*c1[i]))
817 greg 1.7 return(1);
818     return(0);
819     }