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.19 by greg, Tue Apr 10 23:42:52 2018 UTC vs.
Revision 2.24 by greg, Fri Jun 30 00:58:47 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 > 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  
# Line 63 | 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 */
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 100 | 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 108 | Line 125 | oldreadcolrs(                  /* read in an old-style colr scanline
125          FILE  *fp
126   )
127   {
128 <        int  rshift = 0;
128 >        int  rshift = -1;
129          int  i;
130          
131          while (len > 0) {
# Line 118 | Line 135 | oldreadcolrs(                  /* read in an old-style colr scanline
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 >                                rshift >= 0) {
142 >                        i = scanline[0][EXP] << rshift;
143 >                        while (i--) {
144                                  copycolr(scanline[0], scanline[-1]);
145 +                                if (--len <= 0)
146 +                                        return(0);
147                                  scanline++;
127                                len--;
148                          }
149                          rshift += 8;
150                  } else {
# Line 136 | Line 156 | oldreadcolrs(                  /* read in an old-style colr scanline
156          return(0);
157   }
158  
159 <
159 > /*
160 > * There are two scanline formats: old and new.  The old format
161 > * compresses runs of RGBE or XYZE four-byte real pixels; the new
162 > * format breaks the pixels into R, G, B, and E lines (or XYZE lines)
163 > * which are individually run-length encoded.
164 > *
165 > * An old-format scanline always begins with a valid real pixel; at
166 > * least one of the RGB (or XYZ) values will have its high-order bit
167 > * set.  A new-format scanline begins with four bytes which are not a
168 > * valid real pixel: (2, 2, lenhigh, lenlow) where lenhigh is always
169 > * less than 128 and hence never has a high-order bit set.
170 > *
171 > * A new-format scanline is broken into its RGBE or XYZE components.
172 > * Each is output and run-length encoded separately so that a scanline
173 > * is broken into four records.  In turn, each record is organized
174 > * into chunks of up to 128 characters, which begin with a count byte.
175 > * If the count byte is greater than 128, the following data byte is
176 > * repeated (count-128) times.  If not, the count byte is followed by
177 > * that many data bytes.
178 > */
179   int
180   freadcolrs(                     /* read in an encoded colr scanline */
181          COLR  *scanline,
# Line 159 | Line 198 | freadcolrs(                    /* read in an encoded colr scanline */
198          scanline[0][BLU] = getc(fp);
199          if ((i = getc(fp)) == EOF)
200                  return(-1);
201 <        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
201 >        if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
202                  scanline[0][RED] = 2;
203                  scanline[0][EXP] = i;
204                  return(oldreadcolrs(scanline+1, len-1, fp));
# Line 237 | Line 276 | freadscan(                     /* read in a scanline */
276          colr_color(scanline[0], clrscan[0]);
277          while (--len > 0) {
278                  scanline++; clrscan++;
279 <                if (clrscan[0][RED] == clrscan[-1][RED] &&
280 <                            clrscan[0][GRN] == clrscan[-1][GRN] &&
281 <                            clrscan[0][BLU] == clrscan[-1][BLU] &&
282 <                            clrscan[0][EXP] == clrscan[-1][EXP])
279 >                if (clrscan[0][GRN] == clrscan[-1][GRN] &&
280 >                            (clrscan[0][RED] == clrscan[-1][RED]) &
281 >                            (clrscan[0][BLU] == clrscan[-1][BLU]) &
282 >                            (clrscan[0][EXP] == clrscan[-1][EXP]))
283                          copycolor(scanline[0], scanline[-1]);
284                  else
285                          colr_color(scanline[0], clrscan[0]);
# Line 269 | Line 308 | setcolr(                       /* assign a short color value */
308                  return;
309          }
310  
311 <        d = frexp(d, &e) * 255.9999 / d;
311 >        d = frexp(d, &e) * 256.0 / d;
312  
313          if (r > 0.0)
314                  clr[RED] = r * d;
# Line 318 | Line 357 | bigdiff(                               /* c1 delta c2 > md? */
357  
358          for (i = 0; i < 3; i++)
359                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
360 <                        colval(c2,i)-colval(c1,i) > md*colval(c1,i))
360 >                                colval(c2,i)-colval(c1,i) > md*colval(c1,i))
361                          return(1);
362          return(0);
363   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines