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.9 by greg, Mon Nov 6 15:10:24 1989 UTC vs.
Revision 2.10 by greg, Tue Feb 25 02:47:21 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(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 < 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 <
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                          }
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;
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          }
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        }
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 236 | 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]);
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                }
202                  scanline++;
203 <                len--;
203 >                sp++;
204          }
205 <        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);
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--) {
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 <                }
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   {
331        double  frexp();
242          double  d;
243          int  e;
244          
# Line 341 | Line 251 | double  r, g, b;
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;
# Line 350 | Line 260 | double  r, g, b;
260   }
261  
262  
263 + void
264   colr_color(col, clr)            /* convert short to float color */
265   register COLOR  col;
266   register COLR  clr;
# Line 367 | Line 278 | register COLR  clr;
278   }
279  
280  
281 < normcolrs(scan, len)            /* normalize a scanline of colrs */
371 < register COLR  *scan;
372 < int  len;
373 < {
374 <        register int  c;
375 <        register int  shift;
376 <
377 <        while (len-- > 0) {
378 <                shift = scan[0][EXP] - COLXS;
379 <                if (shift > 0) {
380 <                        if (shift > 8) {
381 <                                scan[0][RED] =
382 <                                scan[0][GRN] =
383 <                                scan[0][BLU] = 255;
384 <                        } else {
385 <                                shift--;
386 <                                c = (scan[0][RED]<<1 | 1) << shift;
387 <                                scan[0][RED] = c > 255 ? 255 : c;
388 <                                c = (scan[0][GRN]<<1 | 1) << shift;
389 <                                scan[0][GRN] = c > 255 ? 255 : c;
390 <                                c = (scan[0][BLU]<<1 | 1) << shift;
391 <                                scan[0][BLU] = c > 255 ? 255 : c;
392 <                        }
393 <                } else if (shift < 0) {
394 <                        if (shift < -8) {
395 <                                scan[0][RED] =
396 <                                scan[0][GRN] =
397 <                                scan[0][BLU] = 0;
398 <                        } else {
399 <                                shift = -1-shift;
400 <                                scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
401 <                                scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
402 <                                scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
403 <                        }
404 <                }
405 <                scan[0][EXP] = COLXS;
406 <                scan++;
407 <        }
408 < }
409 <
410 <
281 > int
282   bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
283   register COLOR  c1, c2;
284   double  md;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines