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.25 by greg, Fri Jun 30 15:00:32 2023 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 99 | Line 108 | register FILE  *fp;
108          return(ferror(fp) ? -1 : 0);
109   }
110  
111 <
111 > /*
112 > * An old-format scanline is either a stream of valid RGBE or XYZE real
113 > * pixels or at least one real pixel followed by some number of
114 > * invalid real pixels of the form (1,1,1,n), where n is a count.
115 > * These can themselves be repeated to create a multibyte repeat
116 > * count, with the least significant byte first (little-endian order.)
117 > * Repeat counts are limited by the size of an int; if a repetition
118 > * leads to an overrun, the rest of the the repetition will be
119 > * silently ignored.
120 > */
121   static int
122 < oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
123 < register COLR  *scanline;
124 < int  len;
125 < register FILE  *fp;
122 > oldreadcolrs(                   /* read in an old-style colr scanline */
123 >        COLR  *scanline,
124 >        int  len,
125 >        FILE  *fp
126 > )
127   {
128 <        int  rshift;
129 <        register int  i;
128 >        int  rshift = 0;
129 >        int  i;
130          
112        rshift = 0;
113        
131          while (len > 0) {
132                  scanline[0][RED] = getc(fp);
133                  scanline[0][GRN] = getc(fp);
134                  scanline[0][BLU] = getc(fp);
135 <                scanline[0][EXP] = getc(fp);
136 <                if (feof(fp) || ferror(fp))
135 >                scanline[0][EXP] = i = getc(fp);
136 >                if (i == EOF)
137                          return(-1);
138 <                if (scanline[0][RED] == 1 &&
139 <                                scanline[0][GRN] == 1 &&
140 <                                scanline[0][BLU] == 1) {
141 <                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
138 >                if (scanline[0][GRN] == 1 &&
139 >                                (scanline[0][RED] == 1) &
140 >                                (scanline[0][BLU] == 1)) {
141 >                        i = scanline[0][EXP] << rshift;
142 >                        while (i--) {
143                                  copycolr(scanline[0], scanline[-1]);
144 +                                if (--len <= 0)
145 +                                        return(0);
146                                  scanline++;
127                                len--;
147                          }
148                          rshift += 8;
149                  } else {
# Line 136 | Line 155 | register FILE  *fp;
155          return(0);
156   }
157  
158 <
158 > /*
159 > * There are two scanline formats: old and new.  The old format
160 > * compresses runs of RGBE or XYZE four-byte real pixels; the new
161 > * format breaks the pixels into R, G, B, and E lines (or XYZE lines)
162 > * which are individually run-length encoded.
163 > *
164 > * An old-format scanline always begins with a valid real pixel; at
165 > * least one of the RGB (or XYZ) values will have its high-order bit
166 > * set.  A new-format scanline begins with four bytes which are not a
167 > * valid real pixel: (2, 2, lenhigh, lenlow) where lenhigh is always
168 > * less than 128 and hence never has a high-order bit set.
169 > *
170 > * A new-format scanline is broken into its RGBE or XYZE components.
171 > * Each is output and run-length encoded separately so that a scanline
172 > * is broken into four records.  In turn, each record is organized
173 > * into chunks of up to 128 characters, which begin with a count byte.
174 > * If the count byte is greater than 128, the following data byte is
175 > * repeated (count-128) times.  If not, the count byte is followed by
176 > * that many data bytes.
177 > */
178   int
179 < freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
180 < register COLR  *scanline;
181 < int  len;
182 < register FILE  *fp;
179 > freadcolrs(                     /* read in an encoded colr scanline */
180 >        COLR  *scanline,
181 >        int  len,
182 >        FILE  *fp
183 > )
184   {
185 <        register int  i, j;
185 >        int  i, j;
186          int  code, val;
187                                          /* determine scanline type */
188          if ((len < MINELEN) | (len > MAXELEN))
# Line 158 | Line 197 | register FILE  *fp;
197          scanline[0][BLU] = getc(fp);
198          if ((i = getc(fp)) == EOF)
199                  return(-1);
200 <        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
200 >        if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
201                  scanline[0][RED] = 2;
202                  scanline[0][EXP] = i;
203                  return(oldreadcolrs(scanline+1, len-1, fp));
# Line 174 | Line 213 | register FILE  *fp;
213                      code &= 127;
214                      if ((val = getc(fp)) == EOF)
215                          return -1;
216 +                    if (j + code > len)
217 +                        return -1;      /* overrun */
218                      while (code--)
219                          scanline[j++][i] = val;
220 <                } else                  /* non-run */
220 >                } else {                /* non-run */
221 >                    if (j + code > len)
222 >                        return -1;      /* overrun */
223                      while (code--) {
224                          if ((val = getc(fp)) == EOF)
225                              return -1;
226                          scanline[j++][i] = val;
227                      }
228 +                }
229              }
230          return(0);
231   }
232  
233  
234   int
235 < fwritescan(scanline, len, fp)           /* write out a scanline */
236 < register COLOR  *scanline;
237 < int  len;
238 < FILE  *fp;
235 > fwritescan(                     /* write out a scanline */
236 >        COLOR  *scanline,
237 >        int  len,
238 >        FILE  *fp
239 > )
240   {
241          COLR  *clrscan;
242          int  n;
243 <        register COLR  *sp;
243 >        COLR  *sp;
244                                          /* get scanline buffer */
245          if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
246                  return(-1);
# Line 214 | Line 259 | FILE  *fp;
259  
260  
261   int
262 < freadscan(scanline, len, fp)            /* read in a scanline */
263 < register COLOR  *scanline;
264 < int  len;
265 < FILE  *fp;
262 > freadscan(                      /* read in a scanline */
263 >        COLOR  *scanline,
264 >        int  len,
265 >        FILE  *fp
266 > )
267   {
268 <        register COLR  *clrscan;
268 >        COLR  *clrscan;
269  
270          if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
271                  return(-1);
# Line 229 | Line 275 | FILE  *fp;
275          colr_color(scanline[0], clrscan[0]);
276          while (--len > 0) {
277                  scanline++; clrscan++;
278 <                if (clrscan[0][RED] == clrscan[-1][RED] &&
279 <                            clrscan[0][GRN] == clrscan[-1][GRN] &&
280 <                            clrscan[0][BLU] == clrscan[-1][BLU] &&
281 <                            clrscan[0][EXP] == clrscan[-1][EXP])
278 >                if (clrscan[0][GRN] == clrscan[-1][GRN] &&
279 >                            (clrscan[0][RED] == clrscan[-1][RED]) &
280 >                            (clrscan[0][BLU] == clrscan[-1][BLU]) &
281 >                            (clrscan[0][EXP] == clrscan[-1][EXP]))
282                          copycolor(scanline[0], scanline[-1]);
283                  else
284                          colr_color(scanline[0], clrscan[0]);
# Line 242 | Line 288 | FILE  *fp;
288  
289  
290   void
291 < setcolr(clr, r, g, b)           /* assign a short color value */
292 < register COLR  clr;
293 < double  r, g, b;
291 > setcolr(                        /* assign a short color value */
292 >        COLR  clr,
293 >        double  r,
294 >        double  g,
295 >        double  b
296 > )
297   {
298          double  d;
299          int  e;
# Line 258 | Line 307 | double  r, g, b;
307                  return;
308          }
309  
310 <        d = frexp(d, &e) * 255.9999 / d;
310 >        d = frexp(d, &e) * 256.0 / d;
311  
312          if (r > 0.0)
313                  clr[RED] = r * d;
# Line 278 | Line 327 | double  r, g, b;
327  
328  
329   void
330 < colr_color(col, clr)            /* convert short to float color */
331 < register COLOR  col;
332 < register COLR  clr;
330 > colr_color(                     /* convert short to float color */
331 >        COLOR  col,
332 >        COLR  clr
333 > )
334   {
335          double  f;
336          
# Line 296 | Line 346 | register COLR  clr;
346  
347  
348   int
349 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
350 < register COLOR  c1, c2;
351 < double  md;
349 > bigdiff(                                /* c1 delta c2 > md? */
350 >        COLOR  c1,
351 >        COLOR  c2,
352 >        double  md
353 > )
354   {
355 <        register int  i;
355 >        int  i;
356  
357          for (i = 0; i < 3; i++)
358                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
359 <                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
359 >                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
360                          return(1);
361          return(0);
362   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines