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.2 by greg, Wed Mar 15 13:48:37 1989 UTC vs.
Revision 2.4 by greg, Fri Oct 2 15:59:58 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
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 > #define  MINELEN        8       /* minimum scanline length for encoding */
18 > #define  MINRUN         4       /* minimum run length */
19  
20 < #define  STARTWL        380             /* starting wavelength (nanometers) */
21 < #define  INCWL          10              /* wavelength increment */
22 < #define  NINC           40              /* # of values */
20 > #ifndef frexp
21 > extern double  frexp();
22 > #endif
23  
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 };
24  
25 <
26 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
27 < COLOR  col;
50 < int  s, e;
25 > char *
26 > tempbuffer(len)                 /* get a temporary buffer */
27 > unsigned  len;
28   {
29 <        COLOR  ciecolor;
29 >        extern char  *malloc(), *realloc();
30 >        static char  *tempbuf = NULL;
31 >        static int  tempbuflen = 0;
32  
33 <        spec_cie(ciecolor, s, e);
34 <        cie_rgb(col, ciecolor);
33 >        if (len > tempbuflen) {
34 >                if (tempbuflen > 0)
35 >                        tempbuf = realloc(tempbuf, len);
36 >                else
37 >                        tempbuf = malloc(len);
38 >                tempbuflen = tempbuf==NULL ? 0 : len;
39 >        }
40 >        return(tempbuf);
41   }
42  
43  
44 < spec_cie(col, s, e)             /* compute a color from a spectral range */
45 < COLOR  col;             /* returned color */
46 < int  s, e;              /* starting and ending wavelengths */
44 > fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
45 > register COLR  *scanline;
46 > unsigned  len;
47 > register FILE  *fp;
48   {
49 <        register int  i, d, r;
49 >        register int  i, j, beg, cnt;
50 >        int  c2;
51          
52 <        s -= STARTWL;
53 <        if (s < 0)
54 <                s = 0;
55 <
56 <        e -= STARTWL;
57 <        if (e >= INCWL*(NINC - 1))
58 <                e = INCWL*(NINC - 1) - 1;
59 <
60 <        d = e / INCWL;                  /* interpolate values */
61 <        r = e % INCWL;
62 <        for (i = 0; i < 3; i++)
63 <                col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
64 <
65 <        d = s / INCWL;
66 <        r = s % INCWL;
67 <        for (i = 0; i < 3; i++)
68 <                col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
69 <
70 <        col[RED] = (col[RED] + 0.5) / (256*INCWL);
71 <        col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
72 <        col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
73 < }
74 <
75 <
76 < cie_rgb(rgbcolor, ciecolor)             /* convert CIE to RGB (NTSC) */
77 < register COLOR  rgbcolor, ciecolor;
78 < {
79 <        static float  cmat[3][3] = {
80 <                1.73, -.48, -.26,
81 <                -.81, 1.65, -.02,
82 <                 .08, -.17, 1.28,
83 <        };
84 <        register int  i;
85 <
86 <        for (i = 0; i < 3; i++) {
87 <                rgbcolor[i] =   cmat[i][0]*ciecolor[0] +
88 <                                cmat[i][1]*ciecolor[1] +
89 <                                cmat[i][2]*ciecolor[2] ;
90 <                if (rgbcolor[i] < 0.0)
104 <                        rgbcolor[i] = 0.0;
52 >        if (len < MINELEN | len > 0x7fff)       /* OOBs, write out flat */
53 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
54 >                                        /* put magic header */
55 >        putc(2, fp);
56 >        putc(2, fp);
57 >        putc(len>>8, fp);
58 >        putc(len&255, fp);
59 >                                        /* put components seperately */
60 >        for (i = 0; i < 4; i++) {
61 >            for (j = 0; j < len; j += cnt) {    /* find next run */
62 >                for (beg = j; beg < len; beg += cnt) {
63 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
64 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
65 >                        ;
66 >                    if (cnt >= MINRUN)
67 >                        break;                  /* long enough */
68 >                }
69 >                if (beg-j > 1 && beg-j < MINRUN) {
70 >                    c2 = j+1;
71 >                    while (scanline[c2++][i] == scanline[j][i])
72 >                        if (c2 == beg) {        /* short run */
73 >                            putc(128+beg-j, fp);
74 >                            putc(scanline[j][i], fp);
75 >                            j = beg;
76 >                            break;
77 >                        }
78 >                }
79 >                while (j < beg) {               /* write out non-run */
80 >                    if ((c2 = beg-j) > 128) c2 = 128;
81 >                    putc(c2, fp);
82 >                    while (c2--)
83 >                        putc(scanline[j++][i], fp);
84 >                }
85 >                if (cnt >= MINRUN) {            /* write out run */
86 >                    putc(128+cnt, fp);
87 >                    putc(scanline[beg][i], fp);
88 >                } else
89 >                    cnt = 0;
90 >            }
91          }
92 +        return(ferror(fp) ? -1 : 0);
93   }
107 #endif
94  
95  
96 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
96 > freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
97   register COLR  *scanline;
98   int  len;
99   register FILE  *fp;
100   {
101 <        COLR  lastcolr;
102 <        int  rept;
103 <        
104 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
105 <        lastcolr[EXP] = 0;
106 <        rept = 0;
107 <        
108 <        while (len > 0) {
109 <                if (scanline[0][EXP] == lastcolr[EXP] &&
110 <                                scanline[0][RED] == lastcolr[RED] &&
125 <                                scanline[0][GRN] == lastcolr[GRN] &&
126 <                                scanline[0][BLU] == lastcolr[BLU])
127 <                        rept++;
128 <                else {
129 <                        while (rept) {          /* write out count */
130 <                                putc(1, fp);
131 <                                putc(1, fp);
132 <                                putc(1, fp);
133 <                                putc(rept & 255, fp);
134 <                                rept >>= 8;
135 <                        }
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;
142 <                }
143 <                scanline++;
144 <                len--;
101 >        register int  i, j;
102 >        int  code;
103 >                                        /* determine scanline type */
104 >        if (len < MINELEN | len > 0x7fff)
105 >                return(oldreadcolrs(scanline, len, fp));
106 >        if ((i = getc(fp)) == EOF)
107 >                return(-1);
108 >        if (i != 2) {
109 >                ungetc(i, fp);
110 >                return(oldreadcolrs(scanline, len, fp));
111          }
112 <        while (rept) {          /* write out count */
113 <                putc(1, fp);
114 <                putc(1, fp);
115 <                putc(1, fp);
116 <                putc(rept & 255, fp);
117 <                rept >>= 8;
112 >        scanline[0][GRN] = getc(fp);
113 >        scanline[0][BLU] = getc(fp);
114 >        if ((i = getc(fp)) == EOF)
115 >                return(-1);
116 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
117 >                scanline[0][RED] = 2;
118 >                scanline[0][EXP] = i;
119 >                return(oldreadcolrs(scanline+1, len-1, fp));
120          }
121 <        return(ferror(fp) ? -1 : 0);
121 >        if ((scanline[0][BLU]<<8 | i) != len)
122 >                return(-1);             /* length mismatch! */
123 >                                        /* read each component */
124 >        for (i = 0; i < 4; i++)
125 >            for (j = 0; j < len; ) {
126 >                if ((code = getc(fp)) == EOF)
127 >                    return(-1);
128 >                if (code > 128) {       /* run */
129 >                    scanline[j++][i] = getc(fp);
130 >                    for (code &= 127; --code; j++)
131 >                        scanline[j][i] = scanline[j-1][i];
132 >                } else                  /* non-run */
133 >                    while (code--)
134 >                        scanline[j++][i] = getc(fp);
135 >            }
136 >        return(feof(fp) ? -1 : 0);
137   }
138  
139  
140 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
140 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
141   register COLR  *scanline;
142   int  len;
143   register FILE  *fp;
# Line 193 | Line 176 | register FILE  *fp;
176   fwritescan(scanline, len, fp)           /* write out a scanline */
177   register COLOR  *scanline;
178   int  len;
179 < register FILE  *fp;
179 > FILE  *fp;
180   {
181 <        COLR  lastcolr, thiscolr;
182 <        int  rept;
183 <        
184 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
185 <        lastcolr[EXP] = 0;
186 <        rept = 0;
187 <        
188 <        while (len > 0) {
189 <                setcolr(thiscolr, scanline[0][RED],
181 >        COLR  *clrscan;
182 >        int  n;
183 >        register COLR  *sp;
184 >                                        /* get scanline buffer */
185 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
186 >                return(-1);
187 >        clrscan = sp;
188 >                                        /* convert scanline */
189 >        n = len;
190 >        while (n-- > 0) {
191 >                setcolr(sp[0], scanline[0][RED],
192                                    scanline[0][GRN],
193                                    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                }
194                  scanline++;
195 <                len--;
195 >                sp++;
196          }
197 <        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);
197 >        return(fwritecolrs(clrscan, len, fp));
198   }
199  
200  
201   freadscan(scanline, len, fp)            /* read in a scanline */
202   register COLOR  *scanline;
203   int  len;
204 < register FILE  *fp;
204 > FILE  *fp;
205   {
206 <        COLR  thiscolr;
207 <        int  rshift;
208 <        register int  i;
209 <        
210 <        rshift = 0;
211 <        
212 <        while (len > 0) {
213 <                thiscolr[RED] = getc(fp);
214 <                thiscolr[GRN] = getc(fp);
215 <                thiscolr[BLU] = getc(fp);
216 <                thiscolr[EXP] = getc(fp);
217 <                if (feof(fp) || ferror(fp))
218 <                        return(-1);
219 <                if (thiscolr[RED] == 1 &&
220 <                                thiscolr[GRN] == 1 &&
221 <                                thiscolr[BLU] == 1) {
222 <                        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 <                }
206 >        register COLR  *clrscan;
207 >
208 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
209 >                return(-1);
210 >        if (freadcolrs(clrscan, len, fp) < 0)
211 >                return(-1);
212 >                                        /* convert scanline */
213 >        colr_color(scanline[0], clrscan[0]);
214 >        while (--len > 0) {
215 >                scanline++; clrscan++;
216 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
217 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
218 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
219 >                            clrscan[0][EXP] == clrscan[-1][EXP])
220 >                        copycolor(scanline[0], scanline[-1]);
221 >                else
222 >                        colr_color(scanline[0], clrscan[0]);
223          }
224          return(0);
225   }
# Line 282 | Line 229 | setcolr(clr, r, g, b)          /* assign a short color value *
229   register COLR  clr;
230   double  r, g, b;
231   {
285        double  frexp();
232          double  d;
233          int  e;
234          
235          d = r > g ? r : g;
236          if (b > d) d = b;
237  
238 <        if (d <= 0.0) {
238 >        if (d <= 1e-32) {
239                  clr[RED] = clr[GRN] = clr[BLU] = 0;
240                  clr[EXP] = 0;
241                  return;
# Line 308 | Line 254 | colr_color(col, clr)           /* convert short to float color
254   register COLOR  col;
255   register COLR  clr;
256   {
257 <        double  ldexp(), f;
257 >        double  f;
258          
259          if (clr[EXP] == 0)
260                  col[RED] = col[GRN] = col[BLU] = 0.0;
# Line 321 | Line 267 | register COLR  clr;
267   }
268  
269  
270 < #ifdef  FREXP
271 < double
272 < frexp(x, ip)            /* call it paranoia, I've seen the lib version */
327 < register double  x;
328 < int  *ip;
270 > bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
271 > register COLOR  c1, c2;
272 > double  md;
273   {
330        int  neg;
274          register int  i;
275  
276 <        if (neg = (x < 0.0))
277 <                x = -x;
278 <        else if (x == 0.0) {
279 <                *ip = 0;
280 <                return(0.0);
338 <        }
339 <        if (x < 0.5)
340 <                for (i = 0; x < 0.5; i--)
341 <                        x *= 2.0;
342 <        else
343 <                for (i = 0; x >= 1.0; i++)
344 <                        x /= 2.0;
345 <        *ip = i;
346 <        if (neg)
347 <                return(-x);
348 <        else
349 <                return(x);
276 >        for (i = 0; i < 3; i++)
277 >                if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
278 >                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
279 >                        return(1);
280 >        return(0);
281   }
351 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines