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.3 by greg, Thu May 11 21:22:38 1989 UTC vs.
Revision 2.13 by schorsch, Sun Jul 27 22:12:01 2003 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 "copyright.h"
11 +
12   #include  <stdio.h>
13  
14 < #include  "color.h"
14 > #include  <stdlib.h>
15  
16 < #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 < */
16 > #include  <math.h>
17  
18 < #define  STARTWL        380             /* starting wavelength (nanometers) */
25 < #define  INCWL          10              /* wavelength increment */
26 < #define  NINC           40              /* # of values */
18 > #include  "color.h"
19  
20 < static BYTE  chroma[3][NINC] = {
21 <        {                                                       /* X */
22 <                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 < };
20 > #define  MINELEN        8       /* minimum scanline length for encoding */
21 > #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
22 > #define  MINRUN         4       /* minimum run length */
23  
24  
25 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
26 < COLOR  col;
27 < int  s, e;
25 > char *
26 > tempbuffer(len)                 /* get a temporary buffer */
27 > unsigned int  len;
28   {
29 <        COLOR  ciecolor;
29 >        static char  *tempbuf = NULL;
30 >        static unsigned  tempbuflen = 0;
31  
32 <        spec_cie(ciecolor, s, e);
33 <        cie_rgb(col, ciecolor);
34 < }
35 <
36 <
37 < 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;
32 >        if (len > tempbuflen) {
33 >                if (tempbuflen > 0)
34 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
35 >                else
36 >                        tempbuf = (char *)malloc(len);
37 >                tempbuflen = tempbuf==NULL ? 0 : len;
38          }
39 +        return(tempbuf);
40   }
107 #endif
41  
42  
43 + int
44   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
45   register COLR  *scanline;
46   int  len;
47   register FILE  *fp;
48   {
49 <        COLR  lastcolr;
50 <        int  rept;
49 >        register int  i, j, beg, cnt = 1;
50 >        int  c2;
51          
52 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
53 <        lastcolr[EXP] = 0;
54 <        rept = 0;
55 <        
56 <        while (len > 0) {
57 <                if (scanline[0][EXP] == lastcolr[EXP] &&
58 <                                scanline[0][RED] == lastcolr[RED] &&
59 <                                scanline[0][GRN] == lastcolr[GRN] &&
60 <                                scanline[0][BLU] == lastcolr[BLU])
61 <                        rept++;
62 <                else {
63 <                        while (rept) {          /* write out count */
64 <                                putc(1, fp);
65 <                                putc(1, fp);
66 <                                putc(1, fp);
67 <                                putc(rept & 255, fp);
68 <                                rept >>= 8;
52 >        if ((len < MINELEN) | (len > MAXELEN))  /* 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                          }
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;
78                  }
79 <                scanline++;
80 <                len--;
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          }
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        }
92          return(ferror(fp) ? -1 : 0);
93   }
94  
95  
96 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
96 > static int
97 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
98   register COLR  *scanline;
99   int  len;
100   register FILE  *fp;
# Line 190 | Line 130 | register FILE  *fp;
130   }
131  
132  
133 + int
134 + freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
135 + register COLR  *scanline;
136 + int  len;
137 + register FILE  *fp;
138 + {
139 +        register int  i, j;
140 +        int  code, val;
141 +                                        /* determine scanline type */
142 +        if ((len < MINELEN) | (len > MAXELEN))
143 +                return(oldreadcolrs(scanline, len, fp));
144 +        if ((i = getc(fp)) == EOF)
145 +                return(-1);
146 +        if (i != 2) {
147 +                ungetc(i, fp);
148 +                return(oldreadcolrs(scanline, len, fp));
149 +        }
150 +        scanline[0][GRN] = getc(fp);
151 +        scanline[0][BLU] = getc(fp);
152 +        if ((i = getc(fp)) == EOF)
153 +                return(-1);
154 +        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
155 +                scanline[0][RED] = 2;
156 +                scanline[0][EXP] = i;
157 +                return(oldreadcolrs(scanline+1, len-1, fp));
158 +        }
159 +        if ((scanline[0][BLU]<<8 | i) != len)
160 +                return(-1);             /* length mismatch! */
161 +                                        /* read each component */
162 +        for (i = 0; i < 4; i++)
163 +            for (j = 0; j < len; ) {
164 +                if ((code = getc(fp)) == EOF)
165 +                    return(-1);
166 +                if (code > 128) {       /* run */
167 +                    code &= 127;
168 +                    if ((val = getc(fp)) == EOF)
169 +                        return -1;
170 +                    while (code--)
171 +                        scanline[j++][i] = val;
172 +                } else                  /* non-run */
173 +                    while (code--) {
174 +                        if ((val = getc(fp)) == EOF)
175 +                            return -1;
176 +                        scanline[j++][i] = val;
177 +                    }
178 +            }
179 +        return(0);
180 + }
181 +
182 +
183 + int
184   fwritescan(scanline, len, fp)           /* write out a scanline */
185   register COLOR  *scanline;
186   int  len;
187 < register FILE  *fp;
187 > FILE  *fp;
188   {
189 <        COLR  lastcolr, thiscolr;
190 <        int  rept;
191 <        
192 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
193 <        lastcolr[EXP] = 0;
194 <        rept = 0;
195 <        
196 <        while (len > 0) {
197 <                setcolr(thiscolr, scanline[0][RED],
189 >        COLR  *clrscan;
190 >        int  n;
191 >        register COLR  *sp;
192 >                                        /* get scanline buffer */
193 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
194 >                return(-1);
195 >        clrscan = sp;
196 >                                        /* convert scanline */
197 >        n = len;
198 >        while (n-- > 0) {
199 >                setcolr(sp[0], scanline[0][RED],
200                                    scanline[0][GRN],
201                                    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                }
202                  scanline++;
203 <                len--;
203 >                sp++;
204          }
205 <        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);
205 >        return(fwritecolrs(clrscan, len, fp));
206   }
207  
208  
209 + int
210   freadscan(scanline, len, fp)            /* read in a scanline */
211   register COLOR  *scanline;
212   int  len;
213 < register FILE  *fp;
213 > FILE  *fp;
214   {
215 <        COLR  thiscolr;
216 <        int  rshift;
217 <        register int  i;
218 <        
219 <        rshift = 0;
220 <        
221 <        while (len > 0) {
222 <                thiscolr[RED] = getc(fp);
223 <                thiscolr[GRN] = getc(fp);
224 <                thiscolr[BLU] = getc(fp);
225 <                thiscolr[EXP] = getc(fp);
226 <                if (feof(fp) || ferror(fp))
227 <                        return(-1);
228 <                if (thiscolr[RED] == 1 &&
229 <                                thiscolr[GRN] == 1 &&
230 <                                thiscolr[BLU] == 1) {
231 <                        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 <                }
215 >        register COLR  *clrscan;
216 >
217 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
218 >                return(-1);
219 >        if (freadcolrs(clrscan, len, fp) < 0)
220 >                return(-1);
221 >                                        /* convert scanline */
222 >        colr_color(scanline[0], clrscan[0]);
223 >        while (--len > 0) {
224 >                scanline++; clrscan++;
225 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
226 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
227 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
228 >                            clrscan[0][EXP] == clrscan[-1][EXP])
229 >                        copycolor(scanline[0], scanline[-1]);
230 >                else
231 >                        colr_color(scanline[0], clrscan[0]);
232          }
233          return(0);
234   }
235  
236  
237 + void
238   setcolr(clr, r, g, b)           /* assign a short color value */
239   register COLR  clr;
240   double  r, g, b;
241   {
285        double  frexp();
242          double  d;
243          int  e;
244          
245          d = r > g ? r : g;
246          if (b > d) d = b;
247  
248 <        if (d <= 0.0) {
248 >        if (d <= 1e-32) {
249                  clr[RED] = clr[GRN] = clr[BLU] = 0;
250                  clr[EXP] = 0;
251                  return;
252          }
253  
254 <        d = frexp(d, &e) * 256.0 / d;
254 >        d = frexp(d, &e) * 255.9999 / d;
255  
256 <        clr[RED] = r * d;
257 <        clr[GRN] = g * d;
258 <        clr[BLU] = b * d;
256 >        if (r > 0.0)
257 >                clr[RED] = r * d;
258 >        else
259 >                clr[RED] = 0;
260 >        if (g > 0.0)
261 >                clr[GRN] = g * d;
262 >        else
263 >                clr[GRN] = 0;
264 >        if (b > 0.0)
265 >                clr[BLU] = b * d;
266 >        else
267 >                clr[BLU] = 0;
268 >
269          clr[EXP] = e + COLXS;
270   }
271  
272  
273 + void
274   colr_color(col, clr)            /* convert short to float color */
275   register COLOR  col;
276   register COLR  clr;
277   {
278 <        double  ldexp(), f;
278 >        double  f;
279          
280          if (clr[EXP] == 0)
281                  col[RED] = col[GRN] = col[BLU] = 0.0;
# Line 318 | Line 285 | register COLR  clr;
285                  col[GRN] = (clr[GRN] + 0.5)*f;
286                  col[BLU] = (clr[BLU] + 0.5)*f;
287          }
288 + }
289 +
290 +
291 + int
292 + bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
293 + register COLOR  c1, c2;
294 + double  md;
295 + {
296 +        register int  i;
297 +
298 +        for (i = 0; i < 3; i++)
299 +                if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
300 +                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
301 +                        return(1);
302 +        return(0);
303   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines