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.15 by greg, Tue Sep 14 02:53:50 2004 UTC vs.
Revision 2.22 by greg, Sat Mar 5 17:18:02 2022 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 */
# Line 29 | Line 28 | static const char      RCSid[] = "$Id$";
28   #define  MINRUN         4       /* minimum run length */
29  
30  
31 < char *
32 < tempbuffer(len)                 /* get a temporary buffer */
33 < unsigned int  len;
31 > void *
32 > tempbuffer(                     /* get a temporary buffer */
33 >        unsigned int  len
34 > )
35   {
36 <        static char  *tempbuf = NULL;
37 <        static unsigned  tempbuflen = 0;
36 >        static void             *tempbuf = NULL;
37 >        static unsigned int     tempbuflen = 0;
38  
39 <        if (len > tempbuflen) {
40 <                if (tempbuflen > 0)
41 <                        tempbuf = (char *)realloc((void *)tempbuf, len);
42 <                else
43 <                        tempbuf = (char *)malloc(len);
44 <                tempbuflen = tempbuf==NULL ? 0 : len;
39 >        if (!len) {             /* call to free */
40 >                if (tempbuflen) {
41 >                        free(tempbuf);
42 >                        tempbuf = NULL;
43 >                        tempbuflen = 0;
44 >                }
45 >                return(NULL);
46          }
47 +        if (len <= tempbuflen)  /* big enough already? */
48 +                return(tempbuf);
49 +                                /* else free & reallocate */
50 +        if (tempbuflen)
51 +                free(tempbuf);
52 +        tempbuf = malloc(len);
53 +        tempbuflen = len*(tempbuf != NULL);
54          return(tempbuf);
55   }
56  
57  
58   int
59 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
60 < register COLR  *scanline;
61 < int  len;
62 < register FILE  *fp;
59 > fwritecolrs(                    /* write out a colr scanline */
60 >        COLR  *scanline,
61 >        int  len,
62 >        FILE  *fp
63 > )
64   {
65 <        register int  i, j, beg, cnt = 1;
65 >        int  i, j, beg, cnt = 1;
66          int  c2;
67          
68          if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
# Line 62 | Line 71 | register FILE  *fp;
71          putc(2, fp);
72          putc(2, fp);
73          putc(len>>8, fp);
74 <        putc(len&255, fp);
74 >        putc(len&0xff, fp);
75                                          /* put components seperately */
76          for (i = 0; i < 4; i++) {
77              for (j = 0; j < len; j += cnt) {    /* find next run */
78                  for (beg = j; beg < len; beg += cnt) {
79 <                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
79 >                    for (cnt = 1; (cnt < 127) & (beg+cnt < len) &&
80                              scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
81                          ;
82                      if (cnt >= MINRUN)
83                          break;                  /* long enough */
84                  }
85 <                if (beg-j > 1 && beg-j < MINRUN) {
85 >                if ((beg-j > 1) & (beg-j < MINRUN)) {
86                      c2 = j+1;
87                      while (scanline[c2++][i] == scanline[j][i])
88                          if (c2 == beg) {        /* short run */
# Line 101 | Line 110 | register FILE  *fp;
110  
111  
112   static int
113 < oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
114 < register COLR  *scanline;
115 < int  len;
116 < register FILE  *fp;
113 > oldreadcolrs(                   /* read in an old-style colr scanline */
114 >        COLR  *scanline,
115 >        int  len,
116 >        FILE  *fp
117 > )
118   {
119 <        int  rshift;
120 <        register int  i;
119 >        int  rshift = 0;
120 >        int  i;
121          
112        rshift = 0;
113        
122          while (len > 0) {
123                  scanline[0][RED] = getc(fp);
124                  scanline[0][GRN] = getc(fp);
125                  scanline[0][BLU] = getc(fp);
126 <                scanline[0][EXP] = getc(fp);
127 <                if (feof(fp) || ferror(fp))
126 >                scanline[0][EXP] = i = getc(fp);
127 >                if (i == EOF)
128                          return(-1);
129 <                if (scanline[0][RED] == 1 &&
130 <                                scanline[0][GRN] == 1 &&
131 <                                scanline[0][BLU] == 1) {
132 <                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
129 >                if (scanline[0][GRN] == 1 &&
130 >                                (scanline[0][RED] == 1) &
131 >                                (scanline[0][BLU] == 1)) {
132 >                        i = scanline[0][EXP] << rshift;
133 >                        while (i--) {
134                                  copycolr(scanline[0], scanline[-1]);
135 +                                if (--len <= 0)
136 +                                        return(0);
137                                  scanline++;
127                                len--;
138                          }
139                          rshift += 8;
140                  } else {
# Line 138 | Line 148 | register FILE  *fp;
148  
149  
150   int
151 < freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
152 < register COLR  *scanline;
153 < int  len;
154 < register FILE  *fp;
151 > freadcolrs(                     /* read in an encoded colr scanline */
152 >        COLR  *scanline,
153 >        int  len,
154 >        FILE  *fp
155 > )
156   {
157 <        register int  i, j;
157 >        int  i, j;
158          int  code, val;
159                                          /* determine scanline type */
160          if ((len < MINELEN) | (len > MAXELEN))
# Line 158 | Line 169 | register FILE  *fp;
169          scanline[0][BLU] = getc(fp);
170          if ((i = getc(fp)) == EOF)
171                  return(-1);
172 <        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
172 >        if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
173                  scanline[0][RED] = 2;
174                  scanline[0][EXP] = i;
175                  return(oldreadcolrs(scanline+1, len-1, fp));
# Line 174 | Line 185 | register FILE  *fp;
185                      code &= 127;
186                      if ((val = getc(fp)) == EOF)
187                          return -1;
188 +                    if (j + code > len)
189 +                        return -1;      /* overrun */
190                      while (code--)
191                          scanline[j++][i] = val;
192 <                } else                  /* non-run */
192 >                } else {                /* non-run */
193 >                    if (j + code > len)
194 >                        return -1;      /* overrun */
195                      while (code--) {
196                          if ((val = getc(fp)) == EOF)
197                              return -1;
198                          scanline[j++][i] = val;
199                      }
200 +                }
201              }
202          return(0);
203   }
204  
205  
206   int
207 < fwritescan(scanline, len, fp)           /* write out a scanline */
208 < register COLOR  *scanline;
209 < int  len;
210 < FILE  *fp;
207 > fwritescan(                     /* write out a scanline */
208 >        COLOR  *scanline,
209 >        int  len,
210 >        FILE  *fp
211 > )
212   {
213          COLR  *clrscan;
214          int  n;
215 <        register COLR  *sp;
215 >        COLR  *sp;
216                                          /* get scanline buffer */
217          if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
218                  return(-1);
# Line 214 | Line 231 | FILE  *fp;
231  
232  
233   int
234 < freadscan(scanline, len, fp)            /* read in a scanline */
235 < register COLOR  *scanline;
236 < int  len;
237 < FILE  *fp;
234 > freadscan(                      /* read in a scanline */
235 >        COLOR  *scanline,
236 >        int  len,
237 >        FILE  *fp
238 > )
239   {
240 <        register COLR  *clrscan;
240 >        COLR  *clrscan;
241  
242          if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
243                  return(-1);
# Line 229 | Line 247 | FILE  *fp;
247          colr_color(scanline[0], clrscan[0]);
248          while (--len > 0) {
249                  scanline++; clrscan++;
250 <                if (clrscan[0][RED] == clrscan[-1][RED] &&
251 <                            clrscan[0][GRN] == clrscan[-1][GRN] &&
252 <                            clrscan[0][BLU] == clrscan[-1][BLU] &&
253 <                            clrscan[0][EXP] == clrscan[-1][EXP])
250 >                if (clrscan[0][GRN] == clrscan[-1][GRN] &&
251 >                            (clrscan[0][RED] == clrscan[-1][RED]) &
252 >                            (clrscan[0][BLU] == clrscan[-1][BLU]) &
253 >                            (clrscan[0][EXP] == clrscan[-1][EXP]))
254                          copycolor(scanline[0], scanline[-1]);
255                  else
256                          colr_color(scanline[0], clrscan[0]);
# Line 242 | Line 260 | FILE  *fp;
260  
261  
262   void
263 < setcolr(clr, r, g, b)           /* assign a short color value */
264 < register COLR  clr;
265 < double  r, g, b;
263 > setcolr(                        /* assign a short color value */
264 >        COLR  clr,
265 >        double  r,
266 >        double  g,
267 >        double  b
268 > )
269   {
270          double  d;
271          int  e;
# Line 258 | Line 279 | double  r, g, b;
279                  return;
280          }
281  
282 <        d = frexp(d, &e) * 255.9999 / d;
282 >        d = frexp(d, &e) * 256.0 / d;
283  
284          if (r > 0.0)
285                  clr[RED] = r * d;
# Line 278 | Line 299 | double  r, g, b;
299  
300  
301   void
302 < colr_color(col, clr)            /* convert short to float color */
303 < register COLOR  col;
304 < register COLR  clr;
302 > colr_color(                     /* convert short to float color */
303 >        COLOR  col,
304 >        COLR  clr
305 > )
306   {
307          double  f;
308          
# Line 296 | Line 318 | register COLR  clr;
318  
319  
320   int
321 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
322 < register COLOR  c1, c2;
323 < double  md;
321 > bigdiff(                                /* c1 delta c2 > md? */
322 >        COLOR  c1,
323 >        COLOR  c2,
324 >        double  md
325 > )
326   {
327 <        register int  i;
327 >        int  i;
328  
329          for (i = 0; i < 3; i++)
330                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
331 <                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
331 >                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
332                          return(1);
333          return(0);
334   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines