ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/color.c
(Generate patch)

Comparing ray/src/common/color.c (file contents):
Revision 2.25 by greg, Fri Jun 30 15:00:32 2023 UTC vs.
Revision 2.33 by greg, Wed Jan 17 16:06:56 2024 UTC

# Line 28 | Line 28 | static const char      RCSid[] = "$Id$";
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 +        const 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 +        const 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 + scolr2color(                    /* assign RGB from common exponent */
151 +        COLOR col,
152 +        SCOLR sclr,
153 +        int ncs,
154 +        const float wlpt[4]
155 + )
156 + {
157 +        SCOLOR  scol;
158 +
159 +        scolr2scolor(scol, sclr, ncs);
160 +        scolor2color(col, scol, ncs, wlpt);
161 + }
162 +
163 +
164 + void
165 + scolor2scolr(                   /* float spectrum to common exponent */
166 +        SCOLR sclr,
167 +        SCOLOR scol,
168 +        int ncs
169 + )
170 + {
171 +        int     i = ncs;
172 +        COLORV  p = scol[--i];
173 +
174 +        while (i)
175 +                if (scol[--i] > p)
176 +                        p = scol[i];
177 +        if (p <= 1e-32) {
178 +                memset(sclr, 0, ncs+1);
179 +                return;
180 +        }
181 +        p = frexp(p, &i) * 256.0 / p;
182 +        sclr[ncs] = i + COLXS;
183 +        for (i = ncs; i--; )
184 +                sclr[i] = (scol[i] > 0) * (int)(scol[i]*p);
185 + }
186 +
187 +
188 + void
189 + scolr2scolor(                   /* common exponent to float spectrum */
190 +        SCOLOR scol,
191 +        SCOLR sclr,
192 +        int ncs
193 + )
194 + {
195 +        double  f;
196 +        int     i;
197 +
198 +        if (sclr[ncs] == 0) {
199 +                memset(scol, 0, sizeof(COLORV)*ncs);
200 +                return;
201 +        }
202 +        f = ldexp(1.0, (int)sclr[ncs]-(COLXS+8));
203 +
204 +        for (i = ncs; i--; )
205 +                scol[i] = (sclr[i] + 0.5)*f;
206 + }
207 +
208 +
209 + double
210 + scolor_mean(                    /* compute average for spectral color */
211 +        SCOLOR  scol
212 + )
213 + {
214 +        int     i = NCSAMP;
215 +        double  sum = 0;
216 +
217 +        while (i--)
218 +                sum += scol[i];
219 +
220 +        return sum/(double)NCSAMP;
221 + }
222 +
223 +
224 + double
225 + sintens(                        /* find maximum value from spectrum */
226 +        SCOLOR  scol
227 + )
228 + {
229 +        int     i = NCSAMP;
230 +        COLORV  peak = scol[--i];
231 +
232 +        while (i)
233 +                if (scol[--i] > peak)
234 +                        peak = scol[i];
235 +
236 +        return peak;
237 + }
238 +
239 +
240 + void
241 + convertscolor(                  /* spectrum conversion, zero-fill ends */
242 +        SCOLOR dst,             /* destination spectrum */
243 +        int dnc,                /* destination # of spectral samples/intervals */
244 +        double dwl0,            /* starting destination wavelength (longer) */
245 +        double dwl1,            /* ending destination wavelength (shorter) */
246 +        const COLORV src[],     /* source spectrum array */
247 +        int snc,
248 +        double swl0,            /* long/short wavelengths may be reversed */
249 +        double swl1
250 + )
251 + {
252 +        const int       sdir = 1 - 2*(swl0 < swl1);
253 +        const double    sstp = (swl1 - swl0)/(double)snc;
254 +        const double    dstp = (dwl1 - dwl0)/(double)dnc;
255 +        const double    rdstp = 1./dstp;
256 +        int             si, ssi, di;
257 +        double          wl;
258 +
259 +        if ((dnc < 3) | (dwl0 <= dwl1) | (dst == src))
260 +                return;         /* invalid destination */
261 +
262 +        if (dnc == snc && (dwl0-swl0)*(dwl0-swl0) + (dwl1-swl1)*(dwl1-swl1) <= .5) {
263 +                memcpy(dst, src, sizeof(COLORV)*dnc);
264 +                return;         /* same spectral sampling */
265 +        }
266 +        memset(dst, 0, sizeof(COLORV)*dnc);
267 +                                /* set starting positions */
268 +        if ((sdir>0 ? swl0 : swl1) <= dwl0) {
269 +                if (sdir > 0) {
270 +                        wl = swl0;
271 +                        ssi = 0;
272 +                } else {
273 +                        wl = swl1;
274 +                        ssi = snc-1;
275 +                }
276 +                si = 0;
277 +                di = (wl - dwl0)*rdstp;
278 +        } else {
279 +                wl = dwl0;
280 +                if (sdir > 0) {
281 +                        ssi = si = (wl - swl0)/sstp;
282 +                } else {
283 +                        si = (wl - swl1)/sstp;
284 +                        ssi = snc-1 - si;
285 +                }
286 +                di = 0;
287 +        }
288 +        swl0 += (sdir < 0)*sstp;
289 +                                /* step through intervals */
290 +        while ((si < snc) & (di < dnc)) {
291 +                double  intvl;
292 +                if (swl0 + (ssi+sdir)*sstp < dwl0 + (di+1)*dstp) {
293 +                        intvl = dwl0 + (di+1)*dstp - wl;
294 +                        dst[di++] += src[ssi]*intvl*rdstp;
295 +                } else {
296 +                        intvl = swl0 + (ssi+sdir)*sstp - wl;
297 +                        dst[di] += src[ssi]*intvl*rdstp;
298 +                        ssi += sdir;
299 +                        si++;
300 +                }
301 +                wl += intvl;
302 +        }
303 + }
304 +
305 +
306   void *
307   tempbuffer(                     /* get a temporary buffer */
308 <        unsigned int  len
308 >        size_t  len
309   )
310   {
311 <        static void             *tempbuf = NULL;
312 <        static unsigned int     tempbuflen = 0;
311 >        static void     *tempbuf = NULL;
312 >        static size_t   tempbuflen = 0;
313  
314          if (!len) {             /* call to free */
315                  if (tempbuflen) {
# Line 185 | Line 460 | freadcolrs(                    /* read in an encoded colr scanline */
460          int  i, j;
461          int  code, val;
462                                          /* determine scanline type */
463 <        if ((len < MINELEN) | (len > MAXELEN))
464 <                return(oldreadcolrs(scanline, len, fp));
463 >        if (len <= 0)
464 >                return(0);
465          if ((i = getc(fp)) == EOF)
466                  return(-1);
467 <        if (i != 2) {
193 <                ungetc(i, fp);
194 <                return(oldreadcolrs(scanline, len, fp));
195 <        }
467 >        scanline[0][RED] = i;
468          scanline[0][GRN] = getc(fp);
469          scanline[0][BLU] = getc(fp);
470          if ((i = getc(fp)) == EOF)
471                  return(-1);
472 <        if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
473 <                scanline[0][RED] = 2;
472 >        if ((scanline[0][RED] != 2) | (scanline[0][GRN] != 2) |
473 >                        (scanline[0][BLU] & 0x80)) {
474                  scanline[0][EXP] = i;
475                  return(oldreadcolrs(scanline+1, len-1, fp));
476          }
# Line 231 | Line 503 | freadcolrs(                    /* read in an encoded colr scanline */
503   }
504  
505  
506 + /* read an nc-component common-exponent color scanline */
507   int
508 < fwritescan(                     /* write out a scanline */
508 > freadscolrs(uby8 *scanline, int nc, int len, FILE *fp)
509 > {
510 >        if (nc < 3)
511 >                return(-1);
512 >        if (nc == 3)
513 >                return(freadcolrs((COLR *)scanline, len, fp));
514 >
515 >        if (fread(scanline, nc+1, len, fp) != len)
516 >                return(-1);
517 >        return(0);
518 > }
519 >
520 >
521 > /* write an common-exponent spectral color scanline */
522 > int
523 > fwritescolrs(uby8 *sscanline, int nc, int len, FILE *fp)
524 > {
525 >        if (nc < 3)
526 >                return(-1);
527 >        if (nc == 3)
528 >                return(fwritecolrs((COLR *)sscanline, len, fp));
529 >
530 >        if (fwrite(sscanline, nc+1, len, fp) != len)
531 >                return(-1);
532 >        return(0);
533 > }
534 >
535 >
536 > int
537 > fwritescan(             /* write out an RGB or XYZ scanline */
538          COLOR  *scanline,
539          int  len,
540          FILE  *fp
# Line 259 | Line 561 | fwritescan(                    /* write out a scanline */
561  
562  
563   int
564 < freadscan(                      /* read in a scanline */
564 > freadscan(              /* read in an RGB or XYZ scanline */
565          COLOR  *scanline,
566          int  len,
567          FILE  *fp
# Line 287 | Line 589 | freadscan(                     /* read in a scanline */
589   }
590  
591  
592 + /* read an nc-component color scanline */
593 + int
594 + freadsscan(COLORV *sscanline, int nc, int len, FILE *fp)
595 + {
596 +        uby8    *tscn = (uby8 *)tempbuffer((nc+1)*len);
597 +        int     i;
598 +
599 +        if (tscn == NULL || freadscolrs(tscn, nc, len, fp) < 0)
600 +                return(-1);
601 +        for (i = len; i-- > 0; ) {
602 +                scolr2scolor(sscanline, tscn, nc);
603 +                sscanline += nc;
604 +                tscn += nc+1;
605 +        }
606 +        return(0);
607 + }
608 +
609 +
610 + /* write an nc-component spectral color scanline */
611 + int
612 + fwritesscan(COLORV *sscanline, int nc, int len, FILE *fp)
613 + {
614 +        uby8    *tscn = (uby8 *)tempbuffer((nc+1)*len);
615 +        int     i;
616 +
617 +        if (tscn == NULL)
618 +                return(-1);
619 +        for (i = 0; i < len; i++) {
620 +                scolor2scolr(tscn+i*(nc+1), sscanline, nc);
621 +                sscanline += nc;
622 +        }
623 +        return(fwritescolrs(tscn, nc, len, fp));
624 + }
625 +
626 +
627   void
628   setcolr(                        /* assign a short color value */
629          COLR  clr,
# Line 309 | Line 646 | setcolr(                       /* assign a short color value */
646  
647          d = frexp(d, &e) * 256.0 / d;
648  
649 <        if (r > 0.0)
650 <                clr[RED] = r * d;
651 <        else
315 <                clr[RED] = 0;
316 <        if (g > 0.0)
317 <                clr[GRN] = g * d;
318 <        else
319 <                clr[GRN] = 0;
320 <        if (b > 0.0)
321 <                clr[BLU] = b * d;
322 <        else
323 <                clr[BLU] = 0;
324 <
649 >        clr[RED] = (r > 0) * (int)(r*d);
650 >        clr[GRN] = (g > 0) * (int)(g*d);
651 >        clr[BLU] = (b > 0) * (int)(b*d);
652          clr[EXP] = e + COLXS;
653   }
654  
# Line 334 | Line 661 | colr_color(                    /* convert short to float color */
661   {
662          double  f;
663          
664 <        if (clr[EXP] == 0)
664 >        if (clr[EXP] == 0) {
665                  col[RED] = col[GRN] = col[BLU] = 0.0;
666 <        else {
340 <                f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
341 <                col[RED] = (clr[RED] + 0.5)*f;
342 <                col[GRN] = (clr[GRN] + 0.5)*f;
343 <                col[BLU] = (clr[BLU] + 0.5)*f;
666 >                return;
667          }
668 +        f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
669 +        col[RED] = (clr[RED] + 0.5)*f;
670 +        col[GRN] = (clr[GRN] + 0.5)*f;
671 +        col[BLU] = (clr[BLU] + 0.5)*f;
672   }
673  
674  
# Line 355 | Line 682 | bigdiff(                               /* c1 delta c2 > md? */
682          int  i;
683  
684          for (i = 0; i < 3; i++)
685 <                if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
686 <                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
685 >                if ((colval(c1,i)-colval(c2,i) > md*colval(c2,i)) |
686 >                                (colval(c2,i)-colval(c1,i) > md*colval(c1,i)))
687 >                        return(1);
688 >        return(0);
689 > }
690 >
691 >
692 > int
693 > sbigsdiff(                              /* sc1 delta sc2 > md? */
694 >        SCOLOR  c1,
695 >        SCOLOR  c2,
696 >        double  md
697 > )
698 > {
699 >        int  i = NCSAMP;
700 >
701 >        while (i--)
702 >                if ((c1[i]-c2[i] > md*c2[i]) | (c2[i]-c1[i] > md*c1[i]))
703                          return(1);
704          return(0);
705   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines