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.20 by greg, Sat Feb 23 19:11:25 2019 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);
42 <                else
43 <                        tempbuf = (char *)malloc(len);
37 <                tempbuflen = tempbuf==NULL ? 0 : len;
39 >        if (!len | (len > tempbuflen)) {
40 >                if (tempbuflen)
41 >                        free(tempbuf);
42 >                tempbuf = len ? (char *)malloc(len) : (char *)NULL;
43 >                tempbuflen = len*(tempbuf != NULL);
44          }
45          return(tempbuf);
46   }
47  
48  
49   int
50 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
51 < register COLR  *scanline;
52 < int  len;
53 < register FILE  *fp;
50 > fwritecolrs(                    /* write out a colr scanline */
51 >        COLR  *scanline,
52 >        int  len,
53 >        FILE  *fp
54 > )
55   {
56 <        register int  i, j, beg, cnt = 1;
56 >        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 60 | Line 67 | register FILE  *fp;
67          for (i = 0; i < 4; i++) {
68              for (j = 0; j < len; j += cnt) {    /* find next run */
69                  for (beg = j; beg < len; beg += cnt) {
70 <                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
70 >                    for (cnt = 1; (cnt < 127) & (beg+cnt < len) &&
71                              scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
72                          ;
73                      if (cnt >= MINRUN)
74                          break;                  /* long enough */
75                  }
76 <                if (beg-j > 1 && beg-j < MINRUN) {
76 >                if ((beg-j > 1) & (beg-j < MINRUN)) {
77                      c2 = j+1;
78                      while (scanline[c2++][i] == scanline[j][i])
79                          if (c2 == beg) {        /* short run */
# Line 94 | Line 101 | register FILE  *fp;
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;
104 > oldreadcolrs(                   /* read in an old-style colr scanline */
105 >        COLR  *scanline,
106 >        int  len,
107 >        FILE  *fp
108 > )
109   {
110 <        int  rshift;
111 <        register int  i;
110 >        int  rshift = 0;
111 >        int  i;
112          
105        rshift = 0;
106        
113          while (len > 0) {
114                  scanline[0][RED] = getc(fp);
115                  scanline[0][GRN] = getc(fp);
116                  scanline[0][BLU] = getc(fp);
117 <                scanline[0][EXP] = getc(fp);
118 <                if (feof(fp) || ferror(fp))
117 >                scanline[0][EXP] = i = getc(fp);
118 >                if (i == EOF)
119                          return(-1);
120 <                if (scanline[0][RED] == 1 &&
121 <                                scanline[0][GRN] == 1 &&
122 <                                scanline[0][BLU] == 1) {
123 <                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
120 >                if (scanline[0][GRN] == 1 &&
121 >                                (scanline[0][RED] == 1) &
122 >                                (scanline[0][BLU] == 1)) {
123 >                        i = scanline[0][EXP] << rshift;
124 >                        while (i--) {
125                                  copycolr(scanline[0], scanline[-1]);
126 +                                if (--len <= 0)
127 +                                        return(0);
128                                  scanline++;
120                                len--;
129                          }
130                          rshift += 8;
131                  } else {
# Line 131 | Line 139 | register FILE  *fp;
139  
140  
141   int
142 < freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
143 < register COLR  *scanline;
144 < int  len;
145 < register FILE  *fp;
142 > freadcolrs(                     /* read in an encoded colr scanline */
143 >        COLR  *scanline,
144 >        int  len,
145 >        FILE  *fp
146 > )
147   {
148 <        register int  i, j;
148 >        int  i, j;
149          int  code, val;
150                                          /* determine scanline type */
151 <        if (len < MINELEN | len > MAXELEN)
151 >        if ((len < MINELEN) | (len > MAXELEN))
152                  return(oldreadcolrs(scanline, len, fp));
153          if ((i = getc(fp)) == EOF)
154                  return(-1);
# Line 167 | Line 176 | register FILE  *fp;
176                      code &= 127;
177                      if ((val = getc(fp)) == EOF)
178                          return -1;
179 +                    if (j + code > len)
180 +                        return -1;      /* overrun */
181                      while (code--)
182                          scanline[j++][i] = val;
183 <                } else                  /* non-run */
183 >                } else {                /* non-run */
184 >                    if (j + code > len)
185 >                        return -1;      /* overrun */
186                      while (code--) {
187                          if ((val = getc(fp)) == EOF)
188                              return -1;
189                          scanline[j++][i] = val;
190                      }
191 +                }
192              }
193          return(0);
194   }
195  
196  
197   int
198 < fwritescan(scanline, len, fp)           /* write out a scanline */
199 < register COLOR  *scanline;
200 < int  len;
201 < FILE  *fp;
198 > fwritescan(                     /* write out a scanline */
199 >        COLOR  *scanline,
200 >        int  len,
201 >        FILE  *fp
202 > )
203   {
204          COLR  *clrscan;
205          int  n;
206 <        register COLR  *sp;
206 >        COLR  *sp;
207                                          /* get scanline buffer */
208          if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
209                  return(-1);
# Line 207 | Line 222 | FILE  *fp;
222  
223  
224   int
225 < freadscan(scanline, len, fp)            /* read in a scanline */
226 < register COLOR  *scanline;
227 < int  len;
228 < FILE  *fp;
225 > freadscan(                      /* read in a scanline */
226 >        COLOR  *scanline,
227 >        int  len,
228 >        FILE  *fp
229 > )
230   {
231 <        register COLR  *clrscan;
231 >        COLR  *clrscan;
232  
233          if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
234                  return(-1);
# Line 222 | Line 238 | FILE  *fp;
238          colr_color(scanline[0], clrscan[0]);
239          while (--len > 0) {
240                  scanline++; clrscan++;
241 <                if (clrscan[0][RED] == clrscan[-1][RED] &&
242 <                            clrscan[0][GRN] == clrscan[-1][GRN] &&
243 <                            clrscan[0][BLU] == clrscan[-1][BLU] &&
244 <                            clrscan[0][EXP] == clrscan[-1][EXP])
241 >                if (clrscan[0][GRN] == clrscan[-1][GRN] &&
242 >                            (clrscan[0][RED] == clrscan[-1][RED]) &
243 >                            (clrscan[0][BLU] == clrscan[-1][BLU]) &
244 >                            (clrscan[0][EXP] == clrscan[-1][EXP]))
245                          copycolor(scanline[0], scanline[-1]);
246                  else
247                          colr_color(scanline[0], clrscan[0]);
# Line 235 | Line 251 | FILE  *fp;
251  
252  
253   void
254 < setcolr(clr, r, g, b)           /* assign a short color value */
255 < register COLR  clr;
256 < double  r, g, b;
254 > setcolr(                        /* assign a short color value */
255 >        COLR  clr,
256 >        double  r,
257 >        double  g,
258 >        double  b
259 > )
260   {
261          double  d;
262          int  e;
# Line 253 | Line 272 | double  r, g, b;
272  
273          d = frexp(d, &e) * 255.9999 / d;
274  
275 <        clr[RED] = r * d;
276 <        clr[GRN] = g * d;
277 <        clr[BLU] = b * d;
275 >        if (r > 0.0)
276 >                clr[RED] = r * d;
277 >        else
278 >                clr[RED] = 0;
279 >        if (g > 0.0)
280 >                clr[GRN] = g * d;
281 >        else
282 >                clr[GRN] = 0;
283 >        if (b > 0.0)
284 >                clr[BLU] = b * d;
285 >        else
286 >                clr[BLU] = 0;
287 >
288          clr[EXP] = e + COLXS;
289   }
290  
291  
292   void
293 < colr_color(col, clr)            /* convert short to float color */
294 < register COLOR  col;
295 < register COLR  clr;
293 > colr_color(                     /* convert short to float color */
294 >        COLOR  col,
295 >        COLR  clr
296 > )
297   {
298          double  f;
299          
# Line 279 | Line 309 | register COLR  clr;
309  
310  
311   int
312 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
313 < register COLOR  c1, c2;
314 < double  md;
312 > bigdiff(                                /* c1 delta c2 > md? */
313 >        COLOR  c1,
314 >        COLOR  c2,
315 >        double  md
316 > )
317   {
318 <        register int  i;
318 >        int  i;
319  
320          for (i = 0; i < 3; i++)
321                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
322 <                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
322 >                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
323                          return(1);
324          return(0);
325   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines