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 2.6 by greg, Tue Mar 2 09:09:51 1993 UTC vs.
Revision 2.15 by greg, Tue Sep 14 02:53:50 2004 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 + #undef getc
22 + #undef putc
23 + #define getc    getc_unlocked
24 + #define putc    putc_unlocked
25 + #endif
26 +
27   #define  MINELEN        8       /* minimum scanline length for encoding */
28   #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
29   #define  MINRUN         4       /* minimum run length */
30  
21 #ifndef frexp
22 extern double  frexp();
23 #endif
31  
25
32   char *
33   tempbuffer(len)                 /* get a temporary buffer */
34 < unsigned  len;
34 > unsigned int  len;
35   {
30        extern char  *malloc(), *realloc();
36          static char  *tempbuf = NULL;
37          static unsigned  tempbuflen = 0;
38  
39          if (len > tempbuflen) {
40                  if (tempbuflen > 0)
41 <                        tempbuf = realloc(tempbuf, len);
41 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
42                  else
43 <                        tempbuf = malloc(len);
43 >                        tempbuf = (char *)malloc(len);
44                  tempbuflen = tempbuf==NULL ? 0 : len;
45          }
46          return(tempbuf);
47   }
48  
49  
50 + int
51   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
52   register COLR  *scanline;
53 < unsigned  len;
53 > int  len;
54   register FILE  *fp;
55   {
56 <        register int  i, j, beg, cnt;
56 >        register int  i, j, beg, cnt = 1;
57          int  c2;
58          
59 <        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
59 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
60                  return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
61                                          /* put magic header */
62          putc(2, fp);
# Line 94 | Line 100 | register FILE  *fp;
100   }
101  
102  
103 + static int
104 + oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
105 + register COLR  *scanline;
106 + int  len;
107 + register FILE  *fp;
108 + {
109 +        int  rshift;
110 +        register int  i;
111 +        
112 +        rshift = 0;
113 +        
114 +        while (len > 0) {
115 +                scanline[0][RED] = getc(fp);
116 +                scanline[0][GRN] = getc(fp);
117 +                scanline[0][BLU] = getc(fp);
118 +                scanline[0][EXP] = getc(fp);
119 +                if (feof(fp) || ferror(fp))
120 +                        return(-1);
121 +                if (scanline[0][RED] == 1 &&
122 +                                scanline[0][GRN] == 1 &&
123 +                                scanline[0][BLU] == 1) {
124 +                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
125 +                                copycolr(scanline[0], scanline[-1]);
126 +                                scanline++;
127 +                                len--;
128 +                        }
129 +                        rshift += 8;
130 +                } else {
131 +                        scanline++;
132 +                        len--;
133 +                        rshift = 0;
134 +                }
135 +        }
136 +        return(0);
137 + }
138 +
139 +
140 + int
141   freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
142   register COLR  *scanline;
143   int  len;
# Line 102 | Line 146 | register FILE  *fp;
146          register int  i, j;
147          int  code, val;
148                                          /* determine scanline type */
149 <        if (len < MINELEN | len > MAXELEN)
149 >        if ((len < MINELEN) | (len > MAXELEN))
150                  return(oldreadcolrs(scanline, len, fp));
151          if ((i = getc(fp)) == EOF)
152                  return(-1);
# Line 128 | Line 172 | register FILE  *fp;
172                      return(-1);
173                  if (code > 128) {       /* run */
174                      code &= 127;
175 <                    val = getc(fp);
175 >                    if ((val = getc(fp)) == EOF)
176 >                        return -1;
177                      while (code--)
178                          scanline[j++][i] = val;
179                  } else                  /* non-run */
180 <                    while (code--)
181 <                        scanline[j++][i] = getc(fp);
180 >                    while (code--) {
181 >                        if ((val = getc(fp)) == EOF)
182 >                            return -1;
183 >                        scanline[j++][i] = val;
184 >                    }
185              }
138        return(feof(fp) ? -1 : 0);
139 }
140
141
142 oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
143 register COLR  *scanline;
144 int  len;
145 register FILE  *fp;
146 {
147        int  rshift;
148        register int  i;
149        
150        rshift = 0;
151        
152        while (len > 0) {
153                scanline[0][RED] = getc(fp);
154                scanline[0][GRN] = getc(fp);
155                scanline[0][BLU] = getc(fp);
156                scanline[0][EXP] = getc(fp);
157                if (feof(fp) || ferror(fp))
158                        return(-1);
159                if (scanline[0][RED] == 1 &&
160                                scanline[0][GRN] == 1 &&
161                                scanline[0][BLU] == 1) {
162                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
163                                copycolr(scanline[0], scanline[-1]);
164                                scanline++;
165                                len--;
166                        }
167                        rshift += 8;
168                } else {
169                        scanline++;
170                        len--;
171                        rshift = 0;
172                }
173        }
186          return(0);
187   }
188  
189  
190 + int
191   fwritescan(scanline, len, fp)           /* write out a scanline */
192   register COLOR  *scanline;
193   int  len;
# Line 200 | Line 213 | FILE  *fp;
213   }
214  
215  
216 + int
217   freadscan(scanline, len, fp)            /* read in a scanline */
218   register COLOR  *scanline;
219   int  len;
# Line 227 | Line 241 | FILE  *fp;
241   }
242  
243  
244 + void
245   setcolr(clr, r, g, b)           /* assign a short color value */
246   register COLR  clr;
247   double  r, g, b;
# Line 243 | Line 258 | double  r, g, b;
258                  return;
259          }
260  
261 <        d = frexp(d, &e) * 256.0 / d;
261 >        d = frexp(d, &e) * 255.9999 / d;
262  
263 <        clr[RED] = r * d;
264 <        clr[GRN] = g * d;
265 <        clr[BLU] = b * d;
263 >        if (r > 0.0)
264 >                clr[RED] = r * d;
265 >        else
266 >                clr[RED] = 0;
267 >        if (g > 0.0)
268 >                clr[GRN] = g * d;
269 >        else
270 >                clr[GRN] = 0;
271 >        if (b > 0.0)
272 >                clr[BLU] = b * d;
273 >        else
274 >                clr[BLU] = 0;
275 >
276          clr[EXP] = e + COLXS;
277   }
278  
279  
280 + void
281   colr_color(col, clr)            /* convert short to float color */
282   register COLOR  col;
283   register COLR  clr;
# Line 269 | Line 295 | register COLR  clr;
295   }
296  
297  
298 + int
299   bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
300   register COLOR  c1, c2;
301   double  md;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines