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.10 by greg, Tue Feb 25 02:47:21 2003 UTC vs.
Revision 2.19 by greg, Tue Apr 10 23:42:52 2018 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10   #include "copyright.h"
11  
12   #include  <stdio.h>
13
13   #include  <stdlib.h>
15
14   #include  <math.h>
17
15   #include  "color.h"
16  
17 + #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
18 + #undef getc
19 + #undef putc
20 + #undef ferror
21 + #define getc    getc_unlocked
22 + #define putc    putc_unlocked
23 + #define ferror  ferror_unlocked
24 + #endif
25 +
26   #define  MINELEN        8       /* minimum scanline length for encoding */
27   #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
28   #define  MINRUN         4       /* minimum run length */
29  
30  
31   char *
32 < tempbuffer(len)                 /* get a temporary buffer */
33 < unsigned int  len;
32 > tempbuffer(                     /* get a temporary buffer */
33 >        unsigned int  len
34 > )
35   {
36          static char  *tempbuf = NULL;
37          static unsigned  tempbuflen = 0;
38  
39          if (len > tempbuflen) {
40                  if (tempbuflen > 0)
41 <                        tempbuf = (char *)realloc(tempbuf, len);
41 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
42                  else
43                          tempbuf = (char *)malloc(len);
44                  tempbuflen = tempbuf==NULL ? 0 : len;
# Line 41 | Line 48 | unsigned int  len;
48  
49  
50   int
51 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
52 < register COLR  *scanline;
53 < int  len;
54 < register FILE  *fp;
51 > fwritecolrs(                    /* write out a colr scanline */
52 >        COLR  *scanline,
53 >        int  len,
54 >        FILE  *fp
55 > )
56   {
57 <        register int  i, j, beg, cnt = 1;
57 >        int  i, j, beg, cnt = 1;
58          int  c2;
59          
60 <        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
60 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
61                  return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
62                                          /* put magic header */
63          putc(2, fp);
# Line 94 | Line 102 | register FILE  *fp;
102  
103  
104   static int
105 < oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
106 < register COLR  *scanline;
107 < int  len;
108 < register FILE  *fp;
105 > oldreadcolrs(                   /* read in an old-style colr scanline */
106 >        COLR  *scanline,
107 >        int  len,
108 >        FILE  *fp
109 > )
110   {
111 <        int  rshift;
112 <        register int  i;
111 >        int  rshift = 0;
112 >        int  i;
113          
105        rshift = 0;
106        
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))
118 >                scanline[0][EXP] = i = getc(fp);
119 >                if (i == EOF)
120                          return(-1);
121                  if (scanline[0][RED] == 1 &&
122                                  scanline[0][GRN] == 1 &&
# Line 131 | Line 138 | register FILE  *fp;
138  
139  
140   int
141 < freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
142 < register COLR  *scanline;
143 < int  len;
144 < register FILE  *fp;
141 > freadcolrs(                     /* read in an encoded colr scanline */
142 >        COLR  *scanline,
143 >        int  len,
144 >        FILE  *fp
145 > )
146   {
147 <        register int  i, j;
147 >        int  i, j;
148          int  code, val;
149                                          /* determine scanline type */
150 <        if (len < MINELEN | len > MAXELEN)
150 >        if ((len < MINELEN) | (len > MAXELEN))
151                  return(oldreadcolrs(scanline, len, fp));
152          if ((i = getc(fp)) == EOF)
153                  return(-1);
# Line 167 | Line 175 | register FILE  *fp;
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 */
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(scanline, len, fp)           /* write out a scanline */
198 < register COLOR  *scanline;
199 < int  len;
200 < FILE  *fp;
197 > fwritescan(                     /* write out a scanline */
198 >        COLOR  *scanline,
199 >        int  len,
200 >        FILE  *fp
201 > )
202   {
203          COLR  *clrscan;
204          int  n;
205 <        register COLR  *sp;
205 >        COLR  *sp;
206                                          /* get scanline buffer */
207          if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
208                  return(-1);
# Line 207 | Line 221 | FILE  *fp;
221  
222  
223   int
224 < freadscan(scanline, len, fp)            /* read in a scanline */
225 < register COLOR  *scanline;
226 < int  len;
227 < FILE  *fp;
224 > freadscan(                      /* read in a scanline */
225 >        COLOR  *scanline,
226 >        int  len,
227 >        FILE  *fp
228 > )
229   {
230 <        register COLR  *clrscan;
230 >        COLR  *clrscan;
231  
232          if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
233                  return(-1);
# Line 235 | Line 250 | FILE  *fp;
250  
251  
252   void
253 < setcolr(clr, r, g, b)           /* assign a short color value */
254 < register COLR  clr;
255 < double  r, g, b;
253 > setcolr(                        /* assign a short color value */
254 >        COLR  clr,
255 >        double  r,
256 >        double  g,
257 >        double  b
258 > )
259   {
260          double  d;
261          int  e;
# Line 253 | Line 271 | double  r, g, b;
271  
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   void
292 < colr_color(col, clr)            /* convert short to float color */
293 < register COLOR  col;
294 < register COLR  clr;
292 > colr_color(                     /* convert short to float color */
293 >        COLOR  col,
294 >        COLR  clr
295 > )
296   {
297          double  f;
298          
# Line 279 | Line 308 | register COLR  clr;
308  
309  
310   int
311 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
312 < register COLOR  c1, c2;
313 < double  md;
311 > bigdiff(                                /* c1 delta c2 > md? */
312 >        COLOR  c1,
313 >        COLOR  c2,
314 >        double  md
315 > )
316   {
317 <        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