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.9 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 2.17 by greg, Sat May 31 20:13:31 2014 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *  Externals declared in color.h
8   */
9  
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 < */
10 > #include "copyright.h"
11  
12   #include  <stdio.h>
68
13   #include  <stdlib.h>
70
14   #include  <math.h>
72
15   #include  "color.h"
16  
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  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  
29   char *
30 < tempbuffer(len)                 /* get a temporary buffer */
31 < unsigned int  len;
30 > tempbuffer(                     /* get a temporary buffer */
31 >        unsigned int  len
32 > )
33   {
34          static char  *tempbuf = NULL;
35          static unsigned  tempbuflen = 0;
36  
37          if (len > tempbuflen) {
38                  if (tempbuflen > 0)
39 <                        tempbuf = (char *)realloc(tempbuf, len);
39 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
40                  else
41                          tempbuf = (char *)malloc(len);
42                  tempbuflen = tempbuf==NULL ? 0 : len;
# Line 96 | Line 46 | unsigned int  len;
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;
49 > fwritecolrs(                    /* write out a colr scanline */
50 >        COLR  *scanline,
51 >        int  len,
52 >        FILE  *fp
53 > )
54   {
55 <        register int  i, j, beg, cnt = 1;
55 >        int  i, j, beg, cnt = 1;
56          int  c2;
57          
58 <        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
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);
# Line 149 | Line 100 | register FILE  *fp;
100  
101  
102   static int
103 < oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
104 < register COLR  *scanline;
105 < int  len;
106 < register FILE  *fp;
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 186 | 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 222 | 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 262 | 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 290 | 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 308 | 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 334 | 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