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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines