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.20 by greg, Sat Feb 23 19:11:25 2019 UTC vs.
Revision 2.26 by greg, Sat Jul 1 01:31:17 2023 UTC

# Line 28 | Line 28 | static const char      RCSid[] = "$Id$";
28   #define  MINRUN         4       /* minimum run length */
29  
30  
31 < char *
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 | (len > tempbuflen)) {
40 <                if (tempbuflen)
39 >        if (!len) {             /* call to free */
40 >                if (tempbuflen) {
41                          free(tempbuf);
42 <                tempbuf = len ? (char *)malloc(len) : (char *)NULL;
43 <                tempbuflen = len*(tempbuf != NULL);
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  
# Line 62 | Line 71 | fwritecolrs(                   /* write out a colr scanline */
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 */
# Line 99 | Line 108 | fwritecolrs(                   /* write out a colr scanline */
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(                   /* read in an old-style colr scanline */
123          COLR  *scanline,
# Line 137 | Line 155 | oldreadcolrs(                  /* read in an old-style colr scanline
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(                     /* read in an encoded colr scanline */
180          COLR  *scanline,
# Line 148 | Line 185 | freadcolrs(                    /* read in an encoded colr scanline */
185          int  i, j;
186          int  code, val;
187                                          /* determine scanline type */
188 <        if ((len < MINELEN) | (len > MAXELEN))
189 <                return(oldreadcolrs(scanline, len, fp));
188 >        if (len <= 0)
189 >                return(0);
190          if ((i = getc(fp)) == EOF)
191                  return(-1);
192 <        if (i != 2) {
156 <                ungetc(i, fp);
157 <                return(oldreadcolrs(scanline, len, fp));
158 <        }
192 >        scanline[0][RED] = i;
193          scanline[0][GRN] = getc(fp);
194          scanline[0][BLU] = getc(fp);
195          if ((i = getc(fp)) == EOF)
196                  return(-1);
197 <        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
198 <                scanline[0][RED] = 2;
197 >        if ((scanline[0][RED] != 2) | (scanline[0][GRN] != 2) |
198 >                        (scanline[0][BLU] & 0x80)) {
199                  scanline[0][EXP] = i;
200                  return(oldreadcolrs(scanline+1, len-1, fp));
201          }
# Line 270 | Line 304 | setcolr(                       /* assign a short color value */
304                  return;
305          }
306  
307 <        d = frexp(d, &e) * 255.9999 / d;
307 >        d = frexp(d, &e) * 256.0 / d;
308  
309          if (r > 0.0)
310                  clr[RED] = r * d;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines