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.7 by greg, Fri Oct 20 16:53:01 1989 UTC vs.
Revision 2.9 by greg, Sat Feb 22 02:07:22 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  <stdio.h>
11 <
12 < #include  "color.h"
13 <
14 < #ifdef  SPEC_RGB
15 < /*
16 < *      The following table contains the CIE tristimulus integrals
17 < *  for X, Y, and Z.  The table is cumulative, so that
18 < *  each color coordinate integrates to 1.
10 > /* ====================================================================
11 > * The Radiance Software License, Version 1.0
12 > *
13 > * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 > * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 > *
16 > * Redistribution and use in source and binary forms, with or without
17 > * modification, are permitted provided that the following conditions
18 > * are met:
19 > *
20 > * 1. Redistributions of source code must retain the above copyright
21 > *         notice, this list of conditions and the following disclaimer.
22 > *
23 > * 2. Redistributions in binary form must reproduce the above copyright
24 > *       notice, this list of conditions and the following disclaimer in
25 > *       the documentation and/or other materials provided with the
26 > *       distribution.
27 > *
28 > * 3. The end-user documentation included with the redistribution,
29 > *           if any, must include the following acknowledgment:
30 > *             "This product includes Radiance software
31 > *                 (http://radsite.lbl.gov/)
32 > *                 developed by the Lawrence Berkeley National Laboratory
33 > *               (http://www.lbl.gov/)."
34 > *       Alternately, this acknowledgment may appear in the software itself,
35 > *       if and wherever such third-party acknowledgments normally appear.
36 > *
37 > * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 > *       and "The Regents of the University of California" must
39 > *       not be used to endorse or promote products derived from this
40 > *       software without prior written permission. For written
41 > *       permission, please contact [email protected].
42 > *
43 > * 5. Products derived from this software may not be called "Radiance",
44 > *       nor may "Radiance" appear in their name, without prior written
45 > *       permission of Lawrence Berkeley National Laboratory.
46 > *
47 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 > * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 > * SUCH DAMAGE.
59 > * ====================================================================
60 > *
61 > * This software consists of voluntary contributions made by many
62 > * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 > * information on Lawrence Berkeley National Laboratory, please see
64 > * <http://www.lbl.gov/>.
65   */
66  
67 < #define  STARTWL        380             /* starting wavelength (nanometers) */
25 < #define  INCWL          10              /* wavelength increment */
26 < #define  NINC           40              /* # of values */
67 > #include  <stdio.h>
68  
69 < 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 < };
69 > #include  <stdlib.h>
70  
71 + #include  <math.h>
72  
73 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
49 < COLOR  col;
50 < int  s, e;
51 < {
52 <        COLOR  ciecolor;
73 > #include  "color.h"
74  
75 <        spec_cie(ciecolor, s, e);
76 <        cie_rgb(col, ciecolor);
77 < }
75 > #define  MINELEN        8       /* minimum scanline length for encoding */
76 > #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
77 > #define  MINRUN         4       /* minimum run length */
78  
79  
80 < spec_cie(col, s, e)             /* compute a color from a spectral range */
81 < COLOR  col;             /* returned color */
82 < int  s, e;              /* starting and ending wavelengths */
80 > char *
81 > tempbuffer(len)                 /* get a temporary buffer */
82 > unsigned int  len;
83   {
84 <        register int  i, d, r;
85 <        
65 <        s -= STARTWL;
66 <        if (s < 0)
67 <                s = 0;
84 >        static char  *tempbuf = NULL;
85 >        static unsigned  tempbuflen = 0;
86  
87 <        e -= STARTWL;
88 <        if (e >= INCWL*(NINC - 1))
89 <                e = INCWL*(NINC - 1) - 1;
90 <
91 <        d = e / INCWL;                  /* interpolate values */
92 <        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;
87 >        if (len > tempbuflen) {
88 >                if (tempbuflen > 0)
89 >                        tempbuf = (char *)realloc(tempbuf, len);
90 >                else
91 >                        tempbuf = (char *)malloc(len);
92 >                tempbuflen = tempbuf==NULL ? 0 : len;
93          }
94 +        return(tempbuf);
95   }
107 #endif
96  
97  
98 < 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 <
98 > int
99   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
100   register COLR  *scanline;
101   int  len;
102   register FILE  *fp;
103   {
104 <        COLR  lastcolr;
105 <        int  rept;
104 >        register int  i, j, beg, cnt = 1;
105 >        int  c2;
106          
107 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
108 <        lastcolr[EXP] = 0;
109 <        rept = 0;
110 <        
111 <        while (len > 0) {
112 <                if (scanline[0][EXP] == lastcolr[EXP] &&
113 <                                scanline[0][RED] == lastcolr[RED] &&
114 <                                scanline[0][GRN] == lastcolr[GRN] &&
115 <                                scanline[0][BLU] == lastcolr[BLU])
116 <                        rept++;
117 <                else {
118 <                        while (rept) {          /* write out count */
119 <                                putc(1, fp);
120 <                                putc(1, fp);
121 <                                putc(1, fp);
122 <                                putc(rept & 255, fp);
123 <                                rept >>= 8;
107 >        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
108 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
109 >                                        /* put magic header */
110 >        putc(2, fp);
111 >        putc(2, fp);
112 >        putc(len>>8, fp);
113 >        putc(len&255, fp);
114 >                                        /* put components seperately */
115 >        for (i = 0; i < 4; i++) {
116 >            for (j = 0; j < len; j += cnt) {    /* find next run */
117 >                for (beg = j; beg < len; beg += cnt) {
118 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
119 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
120 >                        ;
121 >                    if (cnt >= MINRUN)
122 >                        break;                  /* long enough */
123 >                }
124 >                if (beg-j > 1 && beg-j < MINRUN) {
125 >                    c2 = j+1;
126 >                    while (scanline[c2++][i] == scanline[j][i])
127 >                        if (c2 == beg) {        /* short run */
128 >                            putc(128+beg-j, fp);
129 >                            putc(scanline[j][i], fp);
130 >                            j = beg;
131 >                            break;
132                          }
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;
133                  }
134 <                scanline++;
135 <                len--;
134 >                while (j < beg) {               /* write out non-run */
135 >                    if ((c2 = beg-j) > 128) c2 = 128;
136 >                    putc(c2, fp);
137 >                    while (c2--)
138 >                        putc(scanline[j++][i], fp);
139 >                }
140 >                if (cnt >= MINRUN) {            /* write out run */
141 >                    putc(128+cnt, fp);
142 >                    putc(scanline[beg][i], fp);
143 >                } else
144 >                    cnt = 0;
145 >            }
146          }
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        }
147          return(ferror(fp) ? -1 : 0);
148   }
149  
150  
151 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
151 > static int
152 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
153   register COLR  *scanline;
154   int  len;
155   register FILE  *fp;
# Line 236 | Line 185 | register FILE  *fp;
185   }
186  
187  
188 + int
189 + freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
190 + register COLR  *scanline;
191 + int  len;
192 + register FILE  *fp;
193 + {
194 +        register int  i, j;
195 +        int  code, val;
196 +                                        /* determine scanline type */
197 +        if (len < MINELEN | len > MAXELEN)
198 +                return(oldreadcolrs(scanline, len, fp));
199 +        if ((i = getc(fp)) == EOF)
200 +                return(-1);
201 +        if (i != 2) {
202 +                ungetc(i, fp);
203 +                return(oldreadcolrs(scanline, len, fp));
204 +        }
205 +        scanline[0][GRN] = getc(fp);
206 +        scanline[0][BLU] = getc(fp);
207 +        if ((i = getc(fp)) == EOF)
208 +                return(-1);
209 +        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
210 +                scanline[0][RED] = 2;
211 +                scanline[0][EXP] = i;
212 +                return(oldreadcolrs(scanline+1, len-1, fp));
213 +        }
214 +        if ((scanline[0][BLU]<<8 | i) != len)
215 +                return(-1);             /* length mismatch! */
216 +                                        /* read each component */
217 +        for (i = 0; i < 4; i++)
218 +            for (j = 0; j < len; ) {
219 +                if ((code = getc(fp)) == EOF)
220 +                    return(-1);
221 +                if (code > 128) {       /* run */
222 +                    code &= 127;
223 +                    if ((val = getc(fp)) == EOF)
224 +                        return -1;
225 +                    while (code--)
226 +                        scanline[j++][i] = val;
227 +                } else                  /* non-run */
228 +                    while (code--) {
229 +                        if ((val = getc(fp)) == EOF)
230 +                            return -1;
231 +                        scanline[j++][i] = val;
232 +                    }
233 +            }
234 +        return(0);
235 + }
236 +
237 +
238 + int
239   fwritescan(scanline, len, fp)           /* write out a scanline */
240   register COLOR  *scanline;
241   int  len;
242 < register FILE  *fp;
242 > FILE  *fp;
243   {
244 <        COLR  lastcolr, thiscolr;
245 <        int  rept;
246 <        
247 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
248 <        lastcolr[EXP] = 0;
249 <        rept = 0;
250 <        
251 <        while (len > 0) {
252 <                setcolr(thiscolr, scanline[0][RED],
244 >        COLR  *clrscan;
245 >        int  n;
246 >        register COLR  *sp;
247 >                                        /* get scanline buffer */
248 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
249 >                return(-1);
250 >        clrscan = sp;
251 >                                        /* convert scanline */
252 >        n = len;
253 >        while (n-- > 0) {
254 >                setcolr(sp[0], scanline[0][RED],
255                                    scanline[0][GRN],
256                                    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                }
257                  scanline++;
258 <                len--;
258 >                sp++;
259          }
260 <        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);
260 >        return(fwritecolrs(clrscan, len, fp));
261   }
262  
263  
264 + int
265   freadscan(scanline, len, fp)            /* read in a scanline */
266   register COLOR  *scanline;
267   int  len;
268 < register FILE  *fp;
268 > FILE  *fp;
269   {
270 <        COLR  thiscolr;
271 <        int  rshift;
272 <        register int  i;
273 <        
274 <        rshift = 0;
275 <        
276 <        while (len > 0) {
277 <                thiscolr[RED] = getc(fp);
278 <                thiscolr[GRN] = getc(fp);
279 <                thiscolr[BLU] = getc(fp);
280 <                thiscolr[EXP] = getc(fp);
281 <                if (feof(fp) || ferror(fp))
282 <                        return(-1);
283 <                if (thiscolr[RED] == 1 &&
284 <                                thiscolr[GRN] == 1 &&
285 <                                thiscolr[BLU] == 1) {
286 <                        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 <                }
270 >        register COLR  *clrscan;
271 >
272 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
273 >                return(-1);
274 >        if (freadcolrs(clrscan, len, fp) < 0)
275 >                return(-1);
276 >                                        /* convert scanline */
277 >        colr_color(scanline[0], clrscan[0]);
278 >        while (--len > 0) {
279 >                scanline++; clrscan++;
280 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
281 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
282 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
283 >                            clrscan[0][EXP] == clrscan[-1][EXP])
284 >                        copycolor(scanline[0], scanline[-1]);
285 >                else
286 >                        colr_color(scanline[0], clrscan[0]);
287          }
288          return(0);
289   }
290  
291  
292 + void
293   setcolr(clr, r, g, b)           /* assign a short color value */
294   register COLR  clr;
295   double  r, g, b;
296   {
331        double  frexp();
297          double  d;
298          int  e;
299          
# Line 341 | Line 306 | double  r, g, b;
306                  return;
307          }
308  
309 <        d = frexp(d, &e) * 256.0 / d;
309 >        d = frexp(d, &e) * 255.9999 / d;
310  
311          clr[RED] = r * d;
312          clr[GRN] = g * d;
# Line 350 | Line 315 | double  r, g, b;
315   }
316  
317  
318 + void
319   colr_color(col, clr)            /* convert short to float color */
320   register COLOR  col;
321   register COLR  clr;
# Line 368 | Line 334 | register COLR  clr;
334  
335  
336   int
371 colr_norm(clr, nclr)            /* normalize a short color, return shift */
372 COLR  clr, nclr;
373 {
374        register int  c;
375        register int  shift = clr[EXP]-COLXS;
376
377        if (shift > 0) {
378                if (shift >= 8) {
379                        nclr[RED] = nclr[GRN] = nclr[BLU] = 255;
380                } else {
381                        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);
403 }
404
405
337   bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
338   register COLOR  c1, c2;
339   double  md;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines