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.17 by greg, Sat May 31 20:13:31 2014 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  <stdio.h>
10 > #include "copyright.h"
11  
12 + #include  <stdio.h>
13 + #include  <stdlib.h>
14 + #include  <math.h>
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 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
18 > #undef getc
19 > #undef putc
20 > #define getc    getc_unlocked
21 > #define putc    putc_unlocked
22 > #endif
23  
24 < #define  STARTWL        380             /* starting wavelength (nanometers) */
25 < #define  INCWL          10              /* wavelength increment */
26 < #define  NINC           40              /* # of values */
24 > #define  MINELEN        8       /* minimum scanline length for encoding */
25 > #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
26 > #define  MINRUN         4       /* minimum run length */
27  
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 };
28  
29 <
30 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
31 < COLOR  col;
32 < int  s, e;
29 > char *
30 > tempbuffer(                     /* get a temporary buffer */
31 >        unsigned int  len
32 > )
33   {
34 <        COLOR  ciecolor;
34 >        static char  *tempbuf = NULL;
35 >        static unsigned  tempbuflen = 0;
36  
37 <        spec_cie(ciecolor, s, e);
38 <        cie_rgb(col, ciecolor);
39 < }
40 <
41 <
42 < 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;
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 */
49 < register int  ord;
50 < int  xres, yres;
51 < FILE  *fp;
48 > int
49 > fwritecolrs(                    /* write out a colr scanline */
50 >        COLR  *scanline,
51 >        int  len,
52 >        FILE  *fp
53 > )
54   {
55 <        if (ord&YMAJOR)
56 <                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 <
156 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
157 < register COLR  *scanline;
158 < int  len;
159 < register FILE  *fp;
160 < {
161 <        COLR  lastcolr;
162 <        int  rept;
55 >        int  i, j, beg, cnt = 1;
56 >        int  c2;
57          
58 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
59 <        lastcolr[EXP] = 0;
60 <        rept = 0;
61 <        
62 <        while (len > 0) {
63 <                if (scanline[0][EXP] == lastcolr[EXP] &&
64 <                                scanline[0][RED] == lastcolr[RED] &&
65 <                                scanline[0][GRN] == lastcolr[GRN] &&
66 <                                scanline[0][BLU] == lastcolr[BLU])
67 <                        rept++;
68 <                else {
69 <                        while (rept) {          /* write out count */
70 <                                putc(1, fp);
71 <                                putc(1, fp);
72 <                                putc(1, fp);
73 <                                putc(rept & 255, fp);
74 <                                rept >>= 8;
58 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
59 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
60 >                                        /* put magic header */
61 >        putc(2, fp);
62 >        putc(2, fp);
63 >        putc(len>>8, fp);
64 >        putc(len&255, fp);
65 >                                        /* put components seperately */
66 >        for (i = 0; i < 4; i++) {
67 >            for (j = 0; j < len; j += cnt) {    /* find next run */
68 >                for (beg = j; beg < len; beg += cnt) {
69 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
70 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
71 >                        ;
72 >                    if (cnt >= MINRUN)
73 >                        break;                  /* long enough */
74 >                }
75 >                if (beg-j > 1 && beg-j < MINRUN) {
76 >                    c2 = j+1;
77 >                    while (scanline[c2++][i] == scanline[j][i])
78 >                        if (c2 == beg) {        /* short run */
79 >                            putc(128+beg-j, fp);
80 >                            putc(scanline[j][i], fp);
81 >                            j = beg;
82 >                            break;
83                          }
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;
84                  }
85 <                scanline++;
86 <                len--;
85 >                while (j < beg) {               /* write out non-run */
86 >                    if ((c2 = beg-j) > 128) c2 = 128;
87 >                    putc(c2, fp);
88 >                    while (c2--)
89 >                        putc(scanline[j++][i], fp);
90 >                }
91 >                if (cnt >= MINRUN) {            /* write out run */
92 >                    putc(128+cnt, fp);
93 >                    putc(scanline[beg][i], fp);
94 >                } else
95 >                    cnt = 0;
96 >            }
97          }
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        }
98          return(ferror(fp) ? -1 : 0);
99   }
100  
101  
102 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
103 < register COLR  *scanline;
104 < int  len;
105 < register FILE  *fp;
102 > static int
103 > oldreadcolrs(                   /* read in an old colr scanline */
104 >        COLR  *scanline,
105 >        int  len,
106 >        FILE  *fp
107 > )
108   {
109          int  rshift;
110 <        register int  i;
110 >        int  i;
111          
112          rshift = 0;
113          
# Line 236 | Line 137 | register FILE  *fp;
137   }
138  
139  
140 < fwritescan(scanline, len, fp)           /* write out a scanline */
141 < register COLOR  *scanline;
142 < int  len;
143 < register FILE  *fp;
140 > int
141 > freadcolrs(                     /* read in an encoded colr scanline */
142 >        COLR  *scanline,
143 >        int  len,
144 >        FILE  *fp
145 > )
146   {
147 <        COLR  lastcolr, thiscolr;
148 <        int  rept;
149 <        
150 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
151 <        lastcolr[EXP] = 0;
152 <        rept = 0;
153 <        
154 <        while (len > 0) {
155 <                setcolr(thiscolr, scanline[0][RED],
147 >        int  i, j;
148 >        int  code, val;
149 >                                        /* determine scanline type */
150 >        if ((len < MINELEN) | (len > MAXELEN))
151 >                return(oldreadcolrs(scanline, len, fp));
152 >        if ((i = getc(fp)) == EOF)
153 >                return(-1);
154 >        if (i != 2) {
155 >                ungetc(i, fp);
156 >                return(oldreadcolrs(scanline, len, fp));
157 >        }
158 >        scanline[0][GRN] = getc(fp);
159 >        scanline[0][BLU] = getc(fp);
160 >        if ((i = getc(fp)) == EOF)
161 >                return(-1);
162 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
163 >                scanline[0][RED] = 2;
164 >                scanline[0][EXP] = i;
165 >                return(oldreadcolrs(scanline+1, len-1, fp));
166 >        }
167 >        if ((scanline[0][BLU]<<8 | i) != len)
168 >                return(-1);             /* length mismatch! */
169 >                                        /* read each component */
170 >        for (i = 0; i < 4; i++)
171 >            for (j = 0; j < len; ) {
172 >                if ((code = getc(fp)) == EOF)
173 >                    return(-1);
174 >                if (code > 128) {       /* run */
175 >                    code &= 127;
176 >                    if ((val = getc(fp)) == EOF)
177 >                        return -1;
178 >                    if (j + code > len)
179 >                        return -1;      /* overrun */
180 >                    while (code--)
181 >                        scanline[j++][i] = val;
182 >                } else {                /* non-run */
183 >                    if (j + code > len)
184 >                        return -1;      /* overrun */
185 >                    while (code--) {
186 >                        if ((val = getc(fp)) == EOF)
187 >                            return -1;
188 >                        scanline[j++][i] = val;
189 >                    }
190 >                }
191 >            }
192 >        return(0);
193 > }
194 >
195 >
196 > int
197 > fwritescan(                     /* write out a scanline */
198 >        COLOR  *scanline,
199 >        int  len,
200 >        FILE  *fp
201 > )
202 > {
203 >        COLR  *clrscan;
204 >        int  n;
205 >        COLR  *sp;
206 >                                        /* get scanline buffer */
207 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
208 >                return(-1);
209 >        clrscan = sp;
210 >                                        /* convert scanline */
211 >        n = len;
212 >        while (n-- > 0) {
213 >                setcolr(sp[0], scanline[0][RED],
214                                    scanline[0][GRN],
215                                    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                }
216                  scanline++;
217 <                len--;
217 >                sp++;
218          }
219 <        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);
219 >        return(fwritecolrs(clrscan, len, fp));
220   }
221  
222  
223 < freadscan(scanline, len, fp)            /* read in a scanline */
224 < register COLOR  *scanline;
225 < int  len;
226 < register FILE  *fp;
223 > int
224 > freadscan(                      /* read in a scanline */
225 >        COLOR  *scanline,
226 >        int  len,
227 >        FILE  *fp
228 > )
229   {
230 <        COLR  thiscolr;
231 <        int  rshift;
232 <        register int  i;
233 <        
234 <        rshift = 0;
235 <        
236 <        while (len > 0) {
237 <                thiscolr[RED] = getc(fp);
238 <                thiscolr[GRN] = getc(fp);
239 <                thiscolr[BLU] = getc(fp);
240 <                thiscolr[EXP] = getc(fp);
241 <                if (feof(fp) || ferror(fp))
242 <                        return(-1);
243 <                if (thiscolr[RED] == 1 &&
244 <                                thiscolr[GRN] == 1 &&
245 <                                thiscolr[BLU] == 1) {
246 <                        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 <                }
230 >        COLR  *clrscan;
231 >
232 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
233 >                return(-1);
234 >        if (freadcolrs(clrscan, len, fp) < 0)
235 >                return(-1);
236 >                                        /* convert scanline */
237 >        colr_color(scanline[0], clrscan[0]);
238 >        while (--len > 0) {
239 >                scanline++; clrscan++;
240 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
241 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
242 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
243 >                            clrscan[0][EXP] == clrscan[-1][EXP])
244 >                        copycolor(scanline[0], scanline[-1]);
245 >                else
246 >                        colr_color(scanline[0], clrscan[0]);
247          }
248          return(0);
249   }
250  
251  
252 < setcolr(clr, r, g, b)           /* assign a short color value */
253 < register COLR  clr;
254 < double  r, g, b;
252 > void
253 > setcolr(                        /* assign a short color value */
254 >        COLR  clr,
255 >        double  r,
256 >        double  g,
257 >        double  b
258 > )
259   {
331        double  frexp();
260          double  d;
261          int  e;
262          
# Line 341 | Line 269 | double  r, g, b;
269                  return;
270          }
271  
272 <        d = frexp(d, &e) * 256.0 / d;
272 >        d = frexp(d, &e) * 255.9999 / d;
273  
274 <        clr[RED] = r * d;
275 <        clr[GRN] = g * d;
276 <        clr[BLU] = b * d;
274 >        if (r > 0.0)
275 >                clr[RED] = r * d;
276 >        else
277 >                clr[RED] = 0;
278 >        if (g > 0.0)
279 >                clr[GRN] = g * d;
280 >        else
281 >                clr[GRN] = 0;
282 >        if (b > 0.0)
283 >                clr[BLU] = b * d;
284 >        else
285 >                clr[BLU] = 0;
286 >
287          clr[EXP] = e + COLXS;
288   }
289  
290  
291 < colr_color(col, clr)            /* convert short to float color */
292 < register COLOR  col;
293 < register COLR  clr;
291 > void
292 > colr_color(                     /* convert short to float color */
293 >        COLOR  col,
294 >        COLR  clr
295 > )
296   {
297          double  f;
298          
# Line 367 | Line 307 | register COLR  clr;
307   }
308  
309  
310 < normcolrs(scan, len)            /* normalize a scanline of colrs */
311 < register COLR  *scan;
312 < int  len;
310 > int
311 > bigdiff(                                /* c1 delta c2 > md? */
312 >        COLOR  c1,
313 >        COLOR  c2,
314 >        double  md
315 > )
316   {
317 <        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 <
411 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
412 < register COLOR  c1, c2;
413 < double  md;
414 < {
415 <        register int  i;
317 >        int  i;
318  
319          for (i = 0; i < 3; i++)
320                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines