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.6 by greg, Fri Oct 20 16:45:26 1989 UTC vs.
Revision 2.15 by greg, Tue Sep 14 02:53:50 2004 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,
23 <                42,  43,  43,  44,  46,  52,  60,  71,  87,  106,
24 <                128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
25 <                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 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 > #undef getc
22 > #undef putc
23 > #define getc    getc_unlocked
24 > #define putc    putc_unlocked
25 > #endif
26  
27 + #define  MINELEN        8       /* minimum scanline length for encoding */
28 + #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
29 + #define  MINRUN         4       /* minimum run length */
30  
48 spec_rgb(col, s, e)             /* comput RGB color from spectral range */
49 COLOR  col;
50 int  s, e;
51 {
52        COLOR  ciecolor;
31  
32 <        spec_cie(ciecolor, s, e);
33 <        cie_rgb(col, ciecolor);
34 < }
57 <
58 <
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 */
32 > char *
33 > tempbuffer(len)                 /* get a temporary buffer */
34 > unsigned int  len;
35   {
36 <        register int  i, d, r;
37 <        
65 <        s -= STARTWL;
66 <        if (s < 0)
67 <                s = 0;
36 >        static char  *tempbuf = NULL;
37 >        static unsigned  tempbuflen = 0;
38  
39 <        e -= STARTWL;
40 <        if (e >= INCWL*(NINC - 1))
41 <                e = INCWL*(NINC - 1) - 1;
42 <
43 <        d = e / INCWL;                  /* interpolate values */
44 <        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 > tempbuflen) {
40 >                if (tempbuflen > 0)
41 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
42 >                else
43 >                        tempbuf = (char *)malloc(len);
44 >                tempbuflen = tempbuf==NULL ? 0 : len;
45          }
46 +        return(tempbuf);
47   }
107 #endif
48  
49  
50 < fputresolu(ord, xres, yres, fp)         /* put x and y resolution */
111 < register int  ord;
112 < int  xres, yres;
113 < FILE  *fp;
114 < {
115 <        if (ord&YMAJOR)
116 <                fprintf(fp, "%cY %d %cX %d\n",
117 <                                ord&YDECR ? '-' : '+', yres,
118 <                                ord&XDECR ? '-' : '+', xres);
119 <        else
120 <                fprintf(fp, "%cX %d %cY %d\n",
121 <                                ord&XDECR ? '-' : '+', xres,
122 <                                ord&YDECR ? '-' : '+', yres);
123 < }
124 <
125 <
126 < fgetresolu(xrp, yrp, fp)                /* get x and y resolution */
127 < int  *xrp, *yrp;
128 < FILE  *fp;
129 < {
130 <        char  buf[64], *xndx, *yndx;
131 <        register char  *cp;
132 <        register int  ord;
133 <
134 <        if (fgets(buf, sizeof(buf), fp) == NULL)
135 <                return(-1);
136 <        xndx = yndx = NULL;
137 <        for (cp = buf+1; *cp; cp++)
138 <                if (*cp == 'X')
139 <                        xndx = cp;
140 <                else if (*cp == 'Y')
141 <                        yndx = cp;
142 <        if (xndx == NULL || yndx == NULL)
143 <                return(-1);
144 <        ord = 0;
145 <        if (xndx > yndx) ord |= YMAJOR;
146 <        if (xndx[-1] == '-') ord |= XDECR;
147 <        if (yndx[-1] == '-') ord |= YDECR;
148 <        if ((*xrp = atoi(xndx+1)) <= 0)
149 <                return(-1);
150 <        if ((*yrp = atoi(yndx+1)) <= 0)
151 <                return(-1);
152 <        return(ord);
153 < }
154 <
155 <
50 > int
51   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
52   register COLR  *scanline;
53   int  len;
54   register FILE  *fp;
55   {
56 <        COLR  lastcolr;
57 <        int  rept;
56 >        register 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                          }
182                        putc(scanline[0][RED], fp);     /* new color */
183                        putc(scanline[0][GRN], fp);
184                        putc(scanline[0][BLU], fp);
185                        putc(scanline[0][EXP], fp);
186                        copycolr(lastcolr, scanline[0]);
187                        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          }
192        while (rept) {          /* write out count */
193                putc(1, fp);
194                putc(1, fp);
195                putc(1, fp);
196                putc(rept & 255, fp);
197                rept >>= 8;
198        }
99          return(ferror(fp) ? -1 : 0);
100   }
101  
102  
103 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
103 > static int
104 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
105   register COLR  *scanline;
106   int  len;
107   register FILE  *fp;
# Line 236 | Line 137 | register FILE  *fp;
137   }
138  
139  
140 + int
141 + freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
142 + register COLR  *scanline;
143 + int  len;
144 + register FILE  *fp;
145 + {
146 +        register int  i, j;
147 +        int  code, val;
148 +                                        /* determine scanline type */
149 +        if ((len < MINELEN) | (len > MAXELEN))
150 +                return(oldreadcolrs(scanline, len, fp));
151 +        if ((i = getc(fp)) == EOF)
152 +                return(-1);
153 +        if (i != 2) {
154 +                ungetc(i, fp);
155 +                return(oldreadcolrs(scanline, len, fp));
156 +        }
157 +        scanline[0][GRN] = getc(fp);
158 +        scanline[0][BLU] = getc(fp);
159 +        if ((i = getc(fp)) == EOF)
160 +                return(-1);
161 +        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
162 +                scanline[0][RED] = 2;
163 +                scanline[0][EXP] = i;
164 +                return(oldreadcolrs(scanline+1, len-1, fp));
165 +        }
166 +        if ((scanline[0][BLU]<<8 | i) != len)
167 +                return(-1);             /* length mismatch! */
168 +                                        /* read each component */
169 +        for (i = 0; i < 4; i++)
170 +            for (j = 0; j < len; ) {
171 +                if ((code = getc(fp)) == EOF)
172 +                    return(-1);
173 +                if (code > 128) {       /* run */
174 +                    code &= 127;
175 +                    if ((val = getc(fp)) == EOF)
176 +                        return -1;
177 +                    while (code--)
178 +                        scanline[j++][i] = val;
179 +                } else                  /* non-run */
180 +                    while (code--) {
181 +                        if ((val = getc(fp)) == EOF)
182 +                            return -1;
183 +                        scanline[j++][i] = val;
184 +                    }
185 +            }
186 +        return(0);
187 + }
188 +
189 +
190 + int
191   fwritescan(scanline, len, fp)           /* write out a scanline */
192   register COLOR  *scanline;
193   int  len;
194 < register FILE  *fp;
194 > FILE  *fp;
195   {
196 <        COLR  lastcolr, thiscolr;
197 <        int  rept;
198 <        
199 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
200 <        lastcolr[EXP] = 0;
201 <        rept = 0;
202 <        
203 <        while (len > 0) {
204 <                setcolr(thiscolr, scanline[0][RED],
196 >        COLR  *clrscan;
197 >        int  n;
198 >        register COLR  *sp;
199 >                                        /* get scanline buffer */
200 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
201 >                return(-1);
202 >        clrscan = sp;
203 >                                        /* convert scanline */
204 >        n = len;
205 >        while (n-- > 0) {
206 >                setcolr(sp[0], scanline[0][RED],
207                                    scanline[0][GRN],
208                                    scanline[0][BLU]);
255                if (thiscolr[EXP] == lastcolr[EXP] &&
256                                thiscolr[RED] == lastcolr[RED] &&
257                                thiscolr[GRN] == lastcolr[GRN] &&
258                                thiscolr[BLU] == lastcolr[BLU])
259                        rept++;
260                else {
261                        while (rept) {          /* write out count */
262                                putc(1, fp);
263                                putc(1, fp);
264                                putc(1, fp);
265                                putc(rept & 255, fp);
266                                rept >>= 8;
267                        }
268                        putc(thiscolr[RED], fp);        /* new color */
269                        putc(thiscolr[GRN], fp);
270                        putc(thiscolr[BLU], fp);
271                        putc(thiscolr[EXP], fp);
272                        copycolr(lastcolr, thiscolr);
273                        rept = 0;
274                }
209                  scanline++;
210 <                len--;
210 >                sp++;
211          }
212 <        while (rept) {          /* write out count */
279 <                putc(1, fp);
280 <                putc(1, fp);
281 <                putc(1, fp);
282 <                putc(rept & 255, fp);
283 <                rept >>= 8;
284 <        }
285 <        return(ferror(fp) ? -1 : 0);
212 >        return(fwritecolrs(clrscan, len, fp));
213   }
214  
215  
216 + int
217   freadscan(scanline, len, fp)            /* read in a scanline */
218   register COLOR  *scanline;
219   int  len;
220 < register FILE  *fp;
220 > FILE  *fp;
221   {
222 <        COLR  thiscolr;
223 <        int  rshift;
224 <        register int  i;
225 <        
226 <        rshift = 0;
227 <        
228 <        while (len > 0) {
229 <                thiscolr[RED] = getc(fp);
230 <                thiscolr[GRN] = getc(fp);
231 <                thiscolr[BLU] = getc(fp);
232 <                thiscolr[EXP] = getc(fp);
233 <                if (feof(fp) || ferror(fp))
234 <                        return(-1);
235 <                if (thiscolr[RED] == 1 &&
236 <                                thiscolr[GRN] == 1 &&
237 <                                thiscolr[BLU] == 1) {
238 <                        for (i = thiscolr[EXP] << rshift; i > 0; i--) {
311 <                                copycolor(scanline[0], scanline[-1]);
312 <                                scanline++;
313 <                                len--;
314 <                        }
315 <                        rshift += 8;
316 <                } else {
317 <                        colr_color(scanline[0], thiscolr);
318 <                        scanline++;
319 <                        len--;
320 <                        rshift = 0;
321 <                }
222 >        register COLR  *clrscan;
223 >
224 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
225 >                return(-1);
226 >        if (freadcolrs(clrscan, len, fp) < 0)
227 >                return(-1);
228 >                                        /* convert scanline */
229 >        colr_color(scanline[0], clrscan[0]);
230 >        while (--len > 0) {
231 >                scanline++; clrscan++;
232 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
233 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
234 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
235 >                            clrscan[0][EXP] == clrscan[-1][EXP])
236 >                        copycolor(scanline[0], scanline[-1]);
237 >                else
238 >                        colr_color(scanline[0], clrscan[0]);
239          }
240          return(0);
241   }
242  
243  
244 + void
245   setcolr(clr, r, g, b)           /* assign a short color value */
246   register COLR  clr;
247   double  r, g, b;
248   {
331        double  frexp();
249          double  d;
250          int  e;
251          
# Line 341 | Line 258 | double  r, g, b;
258                  return;
259          }
260  
261 <        d = frexp(d, &e) * 256.0 / d;
261 >        d = frexp(d, &e) * 255.9999 / d;
262  
263 <        clr[RED] = r * d;
264 <        clr[GRN] = g * d;
265 <        clr[BLU] = b * d;
263 >        if (r > 0.0)
264 >                clr[RED] = r * d;
265 >        else
266 >                clr[RED] = 0;
267 >        if (g > 0.0)
268 >                clr[GRN] = g * d;
269 >        else
270 >                clr[GRN] = 0;
271 >        if (b > 0.0)
272 >                clr[BLU] = b * d;
273 >        else
274 >                clr[BLU] = 0;
275 >
276          clr[EXP] = e + COLXS;
277   }
278  
279  
280 + void
281   colr_color(col, clr)            /* convert short to float color */
282   register COLOR  col;
283   register COLR  clr;
# Line 368 | Line 296 | register COLR  clr;
296  
297  
298   int
299 < colr_norm(clr, nclr)            /* normalize a short color, return shift */
300 < COLR  clr, nclr;
299 > bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
300 > register COLOR  c1, c2;
301 > double  md;
302   {
303 <        register int  c;
375 <        register int  shift = clr[EXP]-COLXS;
303 >        register int  i;
304  
305 <        if (shift > 0) {
306 <                if (shift >= 8) {
307 <                        nclr[RED] = nclr[GRN] = nclr[BLU] = 255;
308 <                } else {
309 <                        c = clr[RED] << shift;
382 <                        nclr[RED] = c > 255 ? 255 : c;
383 <                        c = clr[GRN] << shift;
384 <                        nclr[GRN] = c > 255 ? 255 : c;
385 <                        c = clr[BLU] << shift;
386 <                        nclr[BLU] = c > 255 ? 255 : c;
387 <                }
388 <        } else if (shift < 0) {
389 <                if (shift <= -8) {
390 <                        nclr[RED] = nclr[GRN] = nclr[BLU] = 0;
391 <                } else {
392 <                        nclr[RED] = clr[RED] >> -shift;
393 <                        nclr[GRN] = clr[GRN] >> -shift;
394 <                        nclr[BLU] = clr[BLU] >> -shift;
395 <                }
396 <        } else {
397 <                nclr[RED] = clr[RED];
398 <                nclr[GRN] = clr[GRN];
399 <                nclr[BLU] = clr[BLU];
400 <        }
401 <        nclr[EXP] = COLXS;
402 <        return(shift);
305 >        for (i = 0; i < 3; i++)
306 >                if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
307 >                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
308 >                        return(1);
309 >        return(0);
310   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines