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 1.4 by greg, Thu May 11 22:15:57 1989 UTC vs.
Revision 2.22 by greg, Sat Mar 5 17:18:02 2022 UTC

# Line 1 | Line 1
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  
28 static BYTE  chroma[3][NINC] = {
29        {                                                       /* X */
30                0,   0,   0,   2,   6,   13,  22,  30,  36,  41,
31                42,  43,  43,  44,  46,  52,  60,  71,  87,  106,
32                128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
33                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
34        }, {                                                    /* Y */
35                0,   0,   0,   0,   0,   1,   2,   4,   7,   11,
36                17,  24,  34,  48,  64,  84,  105, 127, 148, 169,
37                188, 205, 220, 232, 240, 246, 250, 253, 254, 255,
38                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
39        }, {                                                    /* Z */
40                0,   0,   2,   10,  32,  66,  118, 153, 191, 220,
41                237, 246, 251, 253, 254, 255, 255, 255, 255, 255,
42                255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
44        }
45 };
30  
31 <
32 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
33 < COLOR  col;
34 < int  s, e;
31 > void *
32 > tempbuffer(                     /* get a temporary buffer */
33 >        unsigned int  len
34 > )
35   {
36 <        COLOR  ciecolor;
36 >        static void             *tempbuf = NULL;
37 >        static unsigned int     tempbuflen = 0;
38  
39 <        spec_cie(ciecolor, s, e);
40 <        cie_rgb(col, ciecolor);
41 < }
42 <
43 <
44 < spec_cie(col, s, e)             /* compute a color from a spectral range */
45 < COLOR  col;             /* returned color */
61 < int  s, e;              /* starting and ending wavelengths */
62 < {
63 <        register int  i, d, r;
64 <        
65 <        s -= STARTWL;
66 <        if (s < 0)
67 <                s = 0;
68 <
69 <        e -= STARTWL;
70 <        if (e >= INCWL*(NINC - 1))
71 <                e = INCWL*(NINC - 1) - 1;
72 <
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;
77 <
78 <        d = s / INCWL;
79 <        r = s % INCWL;
80 <        for (i = 0; i < 3; i++)
81 <                col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
82 <
83 <        col[RED] = (col[RED] + 0.5) / (256*INCWL);
84 <        col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
85 <        col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
86 < }
87 <
88 <
89 < cie_rgb(rgbcolor, ciecolor)             /* convert CIE to RGB (NTSC) */
90 < register COLOR  rgbcolor, ciecolor;
91 < {
92 <        static float  cmat[3][3] = {
93 <                1.73, -.48, -.26,
94 <                -.81, 1.65, -.02,
95 <                 .08, -.17, 1.28,
96 <        };
97 <        register int  i;
98 <
99 <        for (i = 0; i < 3; i++) {
100 <                rgbcolor[i] =   cmat[i][0]*ciecolor[0] +
101 <                                cmat[i][1]*ciecolor[1] +
102 <                                cmat[i][2]*ciecolor[2] ;
103 <                if (rgbcolor[i] < 0.0)
104 <                        rgbcolor[i] = 0.0;
39 >        if (!len) {             /* call to free */
40 >                if (tempbuflen) {
41 >                        free(tempbuf);
42 >                        tempbuf = NULL;
43 >                        tempbuflen = 0;
44 >                }
45 >                return(NULL);
46          }
47 +        if (len <= tempbuflen)  /* big enough already? */
48 +                return(tempbuf);
49 +                                /* else free & reallocate */
50 +        if (tempbuflen)
51 +                free(tempbuf);
52 +        tempbuf = malloc(len);
53 +        tempbuflen = len*(tempbuf != NULL);
54 +        return(tempbuf);
55   }
107 #endif
56  
57  
58 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
59 < register COLR  *scanline;
60 < int  len;
61 < register FILE  *fp;
58 > int
59 > fwritecolrs(                    /* write out a colr scanline */
60 >        COLR  *scanline,
61 >        int  len,
62 >        FILE  *fp
63 > )
64   {
65 <        COLR  lastcolr;
66 <        int  rept;
65 >        int  i, j, beg, cnt = 1;
66 >        int  c2;
67          
68 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
69 <        lastcolr[EXP] = 0;
70 <        rept = 0;
71 <        
72 <        while (len > 0) {
73 <                if (scanline[0][EXP] == lastcolr[EXP] &&
74 <                                scanline[0][RED] == lastcolr[RED] &&
75 <                                scanline[0][GRN] == lastcolr[GRN] &&
76 <                                scanline[0][BLU] == lastcolr[BLU])
77 <                        rept++;
78 <                else {
79 <                        while (rept) {          /* write out count */
80 <                                putc(1, fp);
81 <                                putc(1, fp);
82 <                                putc(1, fp);
83 <                                putc(rept & 255, fp);
84 <                                rept >>= 8;
68 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
69 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
70 >                                        /* put magic header */
71 >        putc(2, fp);
72 >        putc(2, fp);
73 >        putc(len>>8, fp);
74 >        putc(len&0xff, fp);
75 >                                        /* put components seperately */
76 >        for (i = 0; i < 4; i++) {
77 >            for (j = 0; j < len; j += cnt) {    /* find next run */
78 >                for (beg = j; beg < len; beg += cnt) {
79 >                    for (cnt = 1; (cnt < 127) & (beg+cnt < len) &&
80 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
81 >                        ;
82 >                    if (cnt >= MINRUN)
83 >                        break;                  /* long enough */
84 >                }
85 >                if ((beg-j > 1) & (beg-j < MINRUN)) {
86 >                    c2 = j+1;
87 >                    while (scanline[c2++][i] == scanline[j][i])
88 >                        if (c2 == beg) {        /* short run */
89 >                            putc(128+beg-j, fp);
90 >                            putc(scanline[j][i], fp);
91 >                            j = beg;
92 >                            break;
93                          }
136                        putc(scanline[0][RED], fp);     /* new color */
137                        putc(scanline[0][GRN], fp);
138                        putc(scanline[0][BLU], fp);
139                        putc(scanline[0][EXP], fp);
140                        copycolr(lastcolr, scanline[0]);
141                        rept = 0;
94                  }
95 <                scanline++;
96 <                len--;
95 >                while (j < beg) {               /* write out non-run */
96 >                    if ((c2 = beg-j) > 128) c2 = 128;
97 >                    putc(c2, fp);
98 >                    while (c2--)
99 >                        putc(scanline[j++][i], fp);
100 >                }
101 >                if (cnt >= MINRUN) {            /* write out run */
102 >                    putc(128+cnt, fp);
103 >                    putc(scanline[beg][i], fp);
104 >                } else
105 >                    cnt = 0;
106 >            }
107          }
146        while (rept) {          /* write out count */
147                putc(1, fp);
148                putc(1, fp);
149                putc(1, fp);
150                putc(rept & 255, fp);
151                rept >>= 8;
152        }
108          return(ferror(fp) ? -1 : 0);
109   }
110  
111  
112 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
113 < register COLR  *scanline;
114 < int  len;
115 < register FILE  *fp;
112 > static int
113 > oldreadcolrs(                   /* read in an old-style colr scanline */
114 >        COLR  *scanline,
115 >        int  len,
116 >        FILE  *fp
117 > )
118   {
119 <        int  rshift;
120 <        register int  i;
119 >        int  rshift = 0;
120 >        int  i;
121          
165        rshift = 0;
166        
122          while (len > 0) {
123                  scanline[0][RED] = getc(fp);
124                  scanline[0][GRN] = getc(fp);
125                  scanline[0][BLU] = getc(fp);
126 <                scanline[0][EXP] = getc(fp);
127 <                if (feof(fp) || ferror(fp))
126 >                scanline[0][EXP] = i = getc(fp);
127 >                if (i == EOF)
128                          return(-1);
129 <                if (scanline[0][RED] == 1 &&
130 <                                scanline[0][GRN] == 1 &&
131 <                                scanline[0][BLU] == 1) {
132 <                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
129 >                if (scanline[0][GRN] == 1 &&
130 >                                (scanline[0][RED] == 1) &
131 >                                (scanline[0][BLU] == 1)) {
132 >                        i = scanline[0][EXP] << rshift;
133 >                        while (i--) {
134                                  copycolr(scanline[0], scanline[-1]);
135 +                                if (--len <= 0)
136 +                                        return(0);
137                                  scanline++;
180                                len--;
138                          }
139                          rshift += 8;
140                  } else {
# Line 190 | Line 147 | register FILE  *fp;
147   }
148  
149  
150 < fwritescan(scanline, len, fp)           /* write out a scanline */
151 < register COLOR  *scanline;
152 < int  len;
153 < register FILE  *fp;
150 > int
151 > freadcolrs(                     /* read in an encoded colr scanline */
152 >        COLR  *scanline,
153 >        int  len,
154 >        FILE  *fp
155 > )
156   {
157 <        COLR  lastcolr, thiscolr;
158 <        int  rept;
159 <        
160 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
161 <        lastcolr[EXP] = 0;
162 <        rept = 0;
163 <        
164 <        while (len > 0) {
165 <                setcolr(thiscolr, scanline[0][RED],
157 >        int  i, j;
158 >        int  code, val;
159 >                                        /* determine scanline type */
160 >        if ((len < MINELEN) | (len > MAXELEN))
161 >                return(oldreadcolrs(scanline, len, fp));
162 >        if ((i = getc(fp)) == EOF)
163 >                return(-1);
164 >        if (i != 2) {
165 >                ungetc(i, fp);
166 >                return(oldreadcolrs(scanline, len, fp));
167 >        }
168 >        scanline[0][GRN] = getc(fp);
169 >        scanline[0][BLU] = getc(fp);
170 >        if ((i = getc(fp)) == EOF)
171 >                return(-1);
172 >        if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
173 >                scanline[0][RED] = 2;
174 >                scanline[0][EXP] = i;
175 >                return(oldreadcolrs(scanline+1, len-1, fp));
176 >        }
177 >        if ((scanline[0][BLU]<<8 | i) != len)
178 >                return(-1);             /* length mismatch! */
179 >                                        /* read each component */
180 >        for (i = 0; i < 4; i++)
181 >            for (j = 0; j < len; ) {
182 >                if ((code = getc(fp)) == EOF)
183 >                    return(-1);
184 >                if (code > 128) {       /* run */
185 >                    code &= 127;
186 >                    if ((val = getc(fp)) == EOF)
187 >                        return -1;
188 >                    if (j + code > len)
189 >                        return -1;      /* overrun */
190 >                    while (code--)
191 >                        scanline[j++][i] = val;
192 >                } else {                /* non-run */
193 >                    if (j + code > len)
194 >                        return -1;      /* overrun */
195 >                    while (code--) {
196 >                        if ((val = getc(fp)) == EOF)
197 >                            return -1;
198 >                        scanline[j++][i] = val;
199 >                    }
200 >                }
201 >            }
202 >        return(0);
203 > }
204 >
205 >
206 > int
207 > fwritescan(                     /* write out a scanline */
208 >        COLOR  *scanline,
209 >        int  len,
210 >        FILE  *fp
211 > )
212 > {
213 >        COLR  *clrscan;
214 >        int  n;
215 >        COLR  *sp;
216 >                                        /* get scanline buffer */
217 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
218 >                return(-1);
219 >        clrscan = sp;
220 >                                        /* convert scanline */
221 >        n = len;
222 >        while (n-- > 0) {
223 >                setcolr(sp[0], scanline[0][RED],
224                                    scanline[0][GRN],
225                                    scanline[0][BLU]);
209                if (thiscolr[EXP] == lastcolr[EXP] &&
210                                thiscolr[RED] == lastcolr[RED] &&
211                                thiscolr[GRN] == lastcolr[GRN] &&
212                                thiscolr[BLU] == lastcolr[BLU])
213                        rept++;
214                else {
215                        while (rept) {          /* write out count */
216                                putc(1, fp);
217                                putc(1, fp);
218                                putc(1, fp);
219                                putc(rept & 255, fp);
220                                rept >>= 8;
221                        }
222                        putc(thiscolr[RED], fp);        /* new color */
223                        putc(thiscolr[GRN], fp);
224                        putc(thiscolr[BLU], fp);
225                        putc(thiscolr[EXP], fp);
226                        copycolr(lastcolr, thiscolr);
227                        rept = 0;
228                }
226                  scanline++;
227 <                len--;
227 >                sp++;
228          }
229 <        while (rept) {          /* write out count */
233 <                putc(1, fp);
234 <                putc(1, fp);
235 <                putc(1, fp);
236 <                putc(rept & 255, fp);
237 <                rept >>= 8;
238 <        }
239 <        return(ferror(fp) ? -1 : 0);
229 >        return(fwritecolrs(clrscan, len, fp));
230   }
231  
232  
233 < freadscan(scanline, len, fp)            /* read in a scanline */
234 < register COLOR  *scanline;
235 < int  len;
236 < register FILE  *fp;
233 > int
234 > freadscan(                      /* read in a scanline */
235 >        COLOR  *scanline,
236 >        int  len,
237 >        FILE  *fp
238 > )
239   {
240 <        COLR  thiscolr;
241 <        int  rshift;
242 <        register int  i;
243 <        
244 <        rshift = 0;
245 <        
246 <        while (len > 0) {
247 <                thiscolr[RED] = getc(fp);
248 <                thiscolr[GRN] = getc(fp);
249 <                thiscolr[BLU] = getc(fp);
250 <                thiscolr[EXP] = getc(fp);
251 <                if (feof(fp) || ferror(fp))
252 <                        return(-1);
253 <                if (thiscolr[RED] == 1 &&
254 <                                thiscolr[GRN] == 1 &&
255 <                                thiscolr[BLU] == 1) {
256 <                        for (i = thiscolr[EXP] << rshift; i > 0; i--) {
265 <                                copycolor(scanline[0], scanline[-1]);
266 <                                scanline++;
267 <                                len--;
268 <                        }
269 <                        rshift += 8;
270 <                } else {
271 <                        colr_color(scanline[0], thiscolr);
272 <                        scanline++;
273 <                        len--;
274 <                        rshift = 0;
275 <                }
240 >        COLR  *clrscan;
241 >
242 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
243 >                return(-1);
244 >        if (freadcolrs(clrscan, len, fp) < 0)
245 >                return(-1);
246 >                                        /* convert scanline */
247 >        colr_color(scanline[0], clrscan[0]);
248 >        while (--len > 0) {
249 >                scanline++; clrscan++;
250 >                if (clrscan[0][GRN] == clrscan[-1][GRN] &&
251 >                            (clrscan[0][RED] == clrscan[-1][RED]) &
252 >                            (clrscan[0][BLU] == clrscan[-1][BLU]) &
253 >                            (clrscan[0][EXP] == clrscan[-1][EXP]))
254 >                        copycolor(scanline[0], scanline[-1]);
255 >                else
256 >                        colr_color(scanline[0], clrscan[0]);
257          }
258          return(0);
259   }
260  
261  
262 < setcolr(clr, r, g, b)           /* assign a short color value */
263 < register COLR  clr;
264 < double  r, g, b;
262 > void
263 > setcolr(                        /* assign a short color value */
264 >        COLR  clr,
265 >        double  r,
266 >        double  g,
267 >        double  b
268 > )
269   {
285        double  frexp();
270          double  d;
271          int  e;
272          
# Line 297 | Line 281 | double  r, g, b;
281  
282          d = frexp(d, &e) * 256.0 / d;
283  
284 <        clr[RED] = r * d;
285 <        clr[GRN] = g * d;
286 <        clr[BLU] = b * d;
284 >        if (r > 0.0)
285 >                clr[RED] = r * d;
286 >        else
287 >                clr[RED] = 0;
288 >        if (g > 0.0)
289 >                clr[GRN] = g * d;
290 >        else
291 >                clr[GRN] = 0;
292 >        if (b > 0.0)
293 >                clr[BLU] = b * d;
294 >        else
295 >                clr[BLU] = 0;
296 >
297          clr[EXP] = e + COLXS;
298   }
299  
300  
301 < colr_color(col, clr)            /* convert short to float color */
302 < register COLOR  col;
303 < register COLR  clr;
301 > void
302 > colr_color(                     /* convert short to float color */
303 >        COLOR  col,
304 >        COLR  clr
305 > )
306   {
307 <        double  ldexp(), f;
307 >        double  f;
308          
309          if (clr[EXP] == 0)
310                  col[RED] = col[GRN] = col[BLU] = 0.0;
# Line 318 | Line 314 | register COLR  clr;
314                  col[GRN] = (clr[GRN] + 0.5)*f;
315                  col[BLU] = (clr[BLU] + 0.5)*f;
316          }
317 + }
318 +
319 +
320 + int
321 + bigdiff(                                /* c1 delta c2 > md? */
322 +        COLOR  c1,
323 +        COLOR  c2,
324 +        double  md
325 + )
326 + {
327 +        int  i;
328 +
329 +        for (i = 0; i < 3; i++)
330 +                if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
331 +                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
332 +                        return(1);
333 +        return(0);
334   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines