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.11 by greg, Thu Aug 30 09:13:21 1990 UTC vs.
Revision 2.14 by greg, Tue Dec 9 15:51:42 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,
23 <                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 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 > #define getc    getc_unlocked
22 > #define putc    putc_unlocked
23 > #endif
24  
25 + #define  MINELEN        8       /* minimum scanline length for encoding */
26 + #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
27 + #define  MINRUN         4       /* minimum run length */
28  
48 spec_rgb(col, s, e)             /* compute RGB color from spectral range */
49 COLOR  col;
50 int  s, e;
51 {
52        COLOR  ciecolor;
29  
30 <        spec_cie(ciecolor, s, e);
31 <        cie_rgb(col, ciecolor);
32 < }
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 */
30 > char *
31 > tempbuffer(len)                 /* get a temporary buffer */
32 > unsigned int  len;
33   {
34 <        register int  i, d, r;
35 <        
65 <        s -= STARTWL;
66 <        if (s < 0)
67 <                s = 0;
34 >        static char  *tempbuf = NULL;
35 >        static unsigned  tempbuflen = 0;
36  
37 <        e -= STARTWL;
38 <        if (e >= INCWL*(NINC - 1))
39 <                e = INCWL*(NINC - 1) - 1;
40 <
41 <        d = e / INCWL;                  /* interpolate values */
42 <        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;
37 >        if (len > tempbuflen) {
38 >                if (tempbuflen > 0)
39 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
40 >                else
41 >                        tempbuf = (char *)malloc(len);
42 >                tempbuflen = tempbuf==NULL ? 0 : len;
43          }
44 +        return(tempbuf);
45   }
107 #endif
46  
47  
48 < 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 <
48 > int
49   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
50   register COLR  *scanline;
51   int  len;
52   register FILE  *fp;
53   {
54 <        COLR  lastcolr;
55 <        int  rept;
54 >        register int  i, j, beg, cnt = 1;
55 >        int  c2;
56          
57 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
58 <        lastcolr[EXP] = 0;
59 <        rept = 0;
60 <        
61 <        while (len > 0) {
62 <                if (scanline[0][EXP] == lastcolr[EXP] &&
63 <                                scanline[0][RED] == lastcolr[RED] &&
64 <                                scanline[0][GRN] == lastcolr[GRN] &&
65 <                                scanline[0][BLU] == lastcolr[BLU])
66 <                        rept++;
67 <                else {
68 <                        while (rept) {          /* write out count */
69 <                                putc(1, fp);
70 <                                putc(1, fp);
71 <                                putc(1, fp);
72 <                                putc(rept & 255, fp);
73 <                                rept >>= 8;
57 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
58 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
59 >                                        /* put magic header */
60 >        putc(2, fp);
61 >        putc(2, fp);
62 >        putc(len>>8, fp);
63 >        putc(len&255, fp);
64 >                                        /* put components seperately */
65 >        for (i = 0; i < 4; i++) {
66 >            for (j = 0; j < len; j += cnt) {    /* find next run */
67 >                for (beg = j; beg < len; beg += cnt) {
68 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
69 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
70 >                        ;
71 >                    if (cnt >= MINRUN)
72 >                        break;                  /* long enough */
73 >                }
74 >                if (beg-j > 1 && beg-j < MINRUN) {
75 >                    c2 = j+1;
76 >                    while (scanline[c2++][i] == scanline[j][i])
77 >                        if (c2 == beg) {        /* short run */
78 >                            putc(128+beg-j, fp);
79 >                            putc(scanline[j][i], fp);
80 >                            j = beg;
81 >                            break;
82                          }
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;
83                  }
84 <                scanline++;
85 <                len--;
84 >                while (j < beg) {               /* write out non-run */
85 >                    if ((c2 = beg-j) > 128) c2 = 128;
86 >                    putc(c2, fp);
87 >                    while (c2--)
88 >                        putc(scanline[j++][i], fp);
89 >                }
90 >                if (cnt >= MINRUN) {            /* write out run */
91 >                    putc(128+cnt, fp);
92 >                    putc(scanline[beg][i], fp);
93 >                } else
94 >                    cnt = 0;
95 >            }
96          }
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        }
97          return(ferror(fp) ? -1 : 0);
98   }
99  
100  
101 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
101 > static int
102 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
103   register COLR  *scanline;
104   int  len;
105   register FILE  *fp;
# Line 236 | Line 135 | register FILE  *fp;
135   }
136  
137  
138 + int
139 + freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
140 + register COLR  *scanline;
141 + int  len;
142 + register FILE  *fp;
143 + {
144 +        register int  i, j;
145 +        int  code, val;
146 +                                        /* determine scanline type */
147 +        if ((len < MINELEN) | (len > MAXELEN))
148 +                return(oldreadcolrs(scanline, len, fp));
149 +        if ((i = getc(fp)) == EOF)
150 +                return(-1);
151 +        if (i != 2) {
152 +                ungetc(i, fp);
153 +                return(oldreadcolrs(scanline, len, fp));
154 +        }
155 +        scanline[0][GRN] = getc(fp);
156 +        scanline[0][BLU] = getc(fp);
157 +        if ((i = getc(fp)) == EOF)
158 +                return(-1);
159 +        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
160 +                scanline[0][RED] = 2;
161 +                scanline[0][EXP] = i;
162 +                return(oldreadcolrs(scanline+1, len-1, fp));
163 +        }
164 +        if ((scanline[0][BLU]<<8 | i) != len)
165 +                return(-1);             /* length mismatch! */
166 +                                        /* read each component */
167 +        for (i = 0; i < 4; i++)
168 +            for (j = 0; j < len; ) {
169 +                if ((code = getc(fp)) == EOF)
170 +                    return(-1);
171 +                if (code > 128) {       /* run */
172 +                    code &= 127;
173 +                    if ((val = getc(fp)) == EOF)
174 +                        return -1;
175 +                    while (code--)
176 +                        scanline[j++][i] = val;
177 +                } else                  /* non-run */
178 +                    while (code--) {
179 +                        if ((val = getc(fp)) == EOF)
180 +                            return -1;
181 +                        scanline[j++][i] = val;
182 +                    }
183 +            }
184 +        return(0);
185 + }
186 +
187 +
188 + int
189   fwritescan(scanline, len, fp)           /* write out a scanline */
190   register COLOR  *scanline;
191   int  len;
192 < register FILE  *fp;
192 > FILE  *fp;
193   {
194 <        COLR  lastcolr, thiscolr;
195 <        int  rept;
196 <        
197 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
198 <        lastcolr[EXP] = 0;
199 <        rept = 0;
200 <        
201 <        while (len > 0) {
202 <                setcolr(thiscolr, scanline[0][RED],
194 >        COLR  *clrscan;
195 >        int  n;
196 >        register COLR  *sp;
197 >                                        /* get scanline buffer */
198 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
199 >                return(-1);
200 >        clrscan = sp;
201 >                                        /* convert scanline */
202 >        n = len;
203 >        while (n-- > 0) {
204 >                setcolr(sp[0], scanline[0][RED],
205                                    scanline[0][GRN],
206                                    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                }
207                  scanline++;
208 <                len--;
208 >                sp++;
209          }
210 <        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);
210 >        return(fwritecolrs(clrscan, len, fp));
211   }
212  
213  
214 + int
215   freadscan(scanline, len, fp)            /* read in a scanline */
216   register COLOR  *scanline;
217   int  len;
218 < register FILE  *fp;
218 > FILE  *fp;
219   {
220 <        COLR  thiscolr;
221 <        int  rshift;
222 <        register int  i;
223 <        
224 <        rshift = 0;
225 <        
226 <        while (len > 0) {
227 <                thiscolr[RED] = getc(fp);
228 <                thiscolr[GRN] = getc(fp);
229 <                thiscolr[BLU] = getc(fp);
230 <                thiscolr[EXP] = getc(fp);
231 <                if (feof(fp) || ferror(fp))
232 <                        return(-1);
233 <                if (thiscolr[RED] == 1 &&
234 <                                thiscolr[GRN] == 1 &&
235 <                                thiscolr[BLU] == 1) {
236 <                        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 <                }
220 >        register COLR  *clrscan;
221 >
222 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
223 >                return(-1);
224 >        if (freadcolrs(clrscan, len, fp) < 0)
225 >                return(-1);
226 >                                        /* convert scanline */
227 >        colr_color(scanline[0], clrscan[0]);
228 >        while (--len > 0) {
229 >                scanline++; clrscan++;
230 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
231 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
232 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
233 >                            clrscan[0][EXP] == clrscan[-1][EXP])
234 >                        copycolor(scanline[0], scanline[-1]);
235 >                else
236 >                        colr_color(scanline[0], clrscan[0]);
237          }
238          return(0);
239   }
240  
241  
242 + void
243   setcolr(clr, r, g, b)           /* assign a short color value */
244   register COLR  clr;
245   double  r, g, b;
246   {
331        double  frexp();
247          double  d;
248          int  e;
249          
# Line 341 | Line 256 | double  r, g, b;
256                  return;
257          }
258  
259 <        d = frexp(d, &e) * 256.0 / d;
259 >        d = frexp(d, &e) * 255.9999 / d;
260  
261 <        clr[RED] = r * d;
262 <        clr[GRN] = g * d;
263 <        clr[BLU] = b * d;
261 >        if (r > 0.0)
262 >                clr[RED] = r * d;
263 >        else
264 >                clr[RED] = 0;
265 >        if (g > 0.0)
266 >                clr[GRN] = g * d;
267 >        else
268 >                clr[GRN] = 0;
269 >        if (b > 0.0)
270 >                clr[BLU] = b * d;
271 >        else
272 >                clr[BLU] = 0;
273 >
274          clr[EXP] = e + COLXS;
275   }
276  
277  
278 + void
279   colr_color(col, clr)            /* convert short to float color */
280   register COLOR  col;
281   register COLR  clr;
# Line 367 | Line 293 | register COLR  clr;
293   }
294  
295  
296 < normcolrs(scan, len, adjust)    /* normalize a scanline of colrs */
371 < register COLR  *scan;
372 < int  len;
373 < int  adjust;
374 < {
375 <        register int  c;
376 <        register int  shift;
377 <
378 <        while (len-- > 0) {
379 <                shift = scan[0][EXP] + adjust - COLXS;
380 <                if (shift > 0) {
381 <                        if (shift > 8) {
382 <                                scan[0][RED] =
383 <                                scan[0][GRN] =
384 <                                scan[0][BLU] = 255;
385 <                        } else {
386 <                                shift--;
387 <                                c = (scan[0][RED]<<1 | 1) << shift;
388 <                                scan[0][RED] = c > 255 ? 255 : c;
389 <                                c = (scan[0][GRN]<<1 | 1) << shift;
390 <                                scan[0][GRN] = c > 255 ? 255 : c;
391 <                                c = (scan[0][BLU]<<1 | 1) << shift;
392 <                                scan[0][BLU] = c > 255 ? 255 : c;
393 <                        }
394 <                } else if (shift < 0) {
395 <                        if (shift < -8) {
396 <                                scan[0][RED] =
397 <                                scan[0][GRN] =
398 <                                scan[0][BLU] = 0;
399 <                        } else {
400 <                                shift = -1-shift;
401 <                                scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
402 <                                scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
403 <                                scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
404 <                        }
405 <                }
406 <                scan[0][EXP] = COLXS - adjust;
407 <                scan++;
408 <        }
409 < }
410 <
411 <
296 > int
297   bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
298   register COLOR  c1, c2;
299   double  md;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines