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.15 by greg, Wed Aug 28 08:39:47 1991 UTC vs.
Revision 2.14 by greg, Tue Dec 9 15:51:42 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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  <stdlib.h>
15 +
16 + #include  <math.h>
17 +
18   #include  "color.h"
19  
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  
29  
30   char *
31   tempbuffer(len)                 /* get a temporary buffer */
32 < unsigned  len;
32 > unsigned int  len;
33   {
25        extern char  *malloc(), *realloc();
34          static char  *tempbuf = NULL;
35 <        static int  tempbuflen = 0;
35 >        static unsigned  tempbuflen = 0;
36  
37          if (len > tempbuflen) {
38                  if (tempbuflen > 0)
39 <                        tempbuf = realloc(tempbuf, len);
39 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
40                  else
41 <                        tempbuf = malloc(len);
41 >                        tempbuf = (char *)malloc(len);
42                  tempbuflen = tempbuf==NULL ? 0 : len;
43          }
44          return(tempbuf);
45   }
46  
47  
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 <        register int  i, j, beg, cnt;
54 >        register int  i, j, beg, cnt = 1;
55          int  c2;
56          
57 <        if (len < MINELEN)              /* too small to encode */
57 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
58                  return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
59 <        if (len > 32767)                /* too big! */
51 <                return(-1);
52 <        putc(2, fp);                    /* put magic header */
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 */
# Line 90 | Line 98 | register FILE  *fp;
98   }
99  
100  
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;
106 + {
107 +        int  rshift;
108 +        register int  i;
109 +        
110 +        rshift = 0;
111 +        
112 +        while (len > 0) {
113 +                scanline[0][RED] = getc(fp);
114 +                scanline[0][GRN] = getc(fp);
115 +                scanline[0][BLU] = getc(fp);
116 +                scanline[0][EXP] = getc(fp);
117 +                if (feof(fp) || ferror(fp))
118 +                        return(-1);
119 +                if (scanline[0][RED] == 1 &&
120 +                                scanline[0][GRN] == 1 &&
121 +                                scanline[0][BLU] == 1) {
122 +                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
123 +                                copycolr(scanline[0], scanline[-1]);
124 +                                scanline++;
125 +                                len--;
126 +                        }
127 +                        rshift += 8;
128 +                } else {
129 +                        scanline++;
130 +                        len--;
131 +                        rshift = 0;
132 +                }
133 +        }
134 +        return(0);
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;
145 >        int  code, val;
146                                          /* determine scanline type */
147 <        if (len < MINELEN)
147 >        if ((len < MINELEN) | (len > MAXELEN))
148                  return(oldreadcolrs(scanline, len, fp));
149          if ((i = getc(fp)) == EOF)
150                  return(-1);
# Line 123 | Line 169 | register FILE  *fp;
169                  if ((code = getc(fp)) == EOF)
170                      return(-1);
171                  if (code > 128) {       /* run */
172 <                    scanline[j++][i] = getc(fp);
173 <                    for (code &= 127; --code; j++)
174 <                        scanline[j][i] = scanline[j-1][i];
129 <                } else                  /* non-run */
172 >                    code &= 127;
173 >                    if ((val = getc(fp)) == EOF)
174 >                        return -1;
175                      while (code--)
176 <                        scanline[j++][i] = getc(fp);
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              }
133        return(feof(fp) ? -1 : 0);
134 }
135
136
137 oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
138 register COLR  *scanline;
139 int  len;
140 register FILE  *fp;
141 {
142        int  rshift;
143        register int  i;
144        
145        rshift = 0;
146        
147        while (len > 0) {
148                scanline[0][RED] = getc(fp);
149                scanline[0][GRN] = getc(fp);
150                scanline[0][BLU] = getc(fp);
151                scanline[0][EXP] = getc(fp);
152                if (feof(fp) || ferror(fp))
153                        return(-1);
154                if (scanline[0][RED] == 1 &&
155                                scanline[0][GRN] == 1 &&
156                                scanline[0][BLU] == 1) {
157                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
158                                copycolr(scanline[0], scanline[-1]);
159                                scanline++;
160                                len--;
161                        }
162                        rshift += 8;
163                } else {
164                        scanline++;
165                        len--;
166                        rshift = 0;
167                }
168        }
184          return(0);
185   }
186  
187  
188 + int
189   fwritescan(scanline, len, fp)           /* write out a scanline */
190   register COLOR  *scanline;
191   int  len;
# Line 195 | Line 211 | FILE  *fp;
211   }
212  
213  
214 + int
215   freadscan(scanline, len, fp)            /* read in a scanline */
216   register COLOR  *scanline;
217   int  len;
# Line 222 | Line 239 | FILE  *fp;
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   {
229        double  frexp();
247          double  d;
248          int  e;
249          
# Line 239 | 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 265 | Line 293 | register COLR  clr;
293   }
294  
295  
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