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 1.14 by greg, Fri Aug 23 12:33:49 1991 UTC vs.
Revision 2.20 by greg, Sat Feb 23 19:11:25 2019 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  color.c - routines for color calculations.
6   *
7 < *     10/10/85
7 > *  Externals declared in color.h
8   */
9  
10 < #include  <stdio.h>
10 > #include "copyright.h"
11  
12 + #include  <stdio.h>
13 + #include  <stdlib.h>
14 + #include  <math.h>
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  len;
32 > tempbuffer(                     /* get a temporary buffer */
33 >        unsigned int  len
34 > )
35   {
25        extern char  *malloc(), *realloc();
36          static char  *tempbuf = NULL;
37 <        static int  tempbuflen = 0;
37 >        static unsigned  tempbuflen = 0;
38  
39 <        if (len > tempbuflen) {
40 <                if (tempbuflen > 0)
41 <                        tempbuf = realloc(tempbuf, len);
42 <                else
43 <                        tempbuf = malloc(len);
34 <                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 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
50 < register COLR  *scanline;
51 < int  len;
52 < register FILE  *fp;
49 > int
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;
56 >        int  i, j, beg, cnt = 1;
57          int  c2;
58          
59 <        if (len < MINELEN)              /* too small to encode */
59 >        if ((len < MINELEN) | (len > MAXELEN))  /* OOBs, write out flat */
60                  return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
61 <        if (len > 32767)                /* too big! */
51 <                return(-1);
52 <        putc(2, fp);                    /* put magic header */
61 >                                        /* put magic header */
62          putc(2, fp);
63 +        putc(2, fp);
64          putc(len>>8, fp);
65          putc(len&255, fp);
66                                          /* put components seperately */
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 <                while (j < beg) {               /* write out non-run(s) */
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 */
80 >                            putc(128+beg-j, fp);
81 >                            putc(scanline[j][i], fp);
82 >                            j = beg;
83 >                            break;
84 >                        }
85 >                }
86 >                while (j < beg) {               /* write out non-run */
87                      if ((c2 = beg-j) > 128) c2 = 128;
88                      putc(c2, fp);
89                      while (c2--)
# Line 80 | Line 100 | register FILE  *fp;
100   }
101  
102  
103 < freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
104 < register COLR  *scanline;
105 < int  len;
106 < register FILE  *fp;
103 > static int
104 > oldreadcolrs(                   /* read in an old-style colr scanline */
105 >        COLR  *scanline,
106 >        int  len,
107 >        FILE  *fp
108 > )
109   {
110 <        register int  i, j;
111 <        int  code;
110 >        int  rshift = 0;
111 >        int  i;
112 >        
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] = i = getc(fp);
118 >                if (i == EOF)
119 >                        return(-1);
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++;
129 >                        }
130 >                        rshift += 8;
131 >                } else {
132 >                        scanline++;
133 >                        len--;
134 >                        rshift = 0;
135 >                }
136 >        }
137 >        return(0);
138 > }
139 >
140 >
141 > int
142 > freadcolrs(                     /* read in an encoded colr scanline */
143 >        COLR  *scanline,
144 >        int  len,
145 >        FILE  *fp
146 > )
147 > {
148 >        int  i, j;
149 >        int  code, val;
150                                          /* determine scanline type */
151 <        if (len < MINELEN)
151 >        if ((len < MINELEN) | (len > MAXELEN))
152                  return(oldreadcolrs(scanline, len, fp));
153          if ((i = getc(fp)) == EOF)
154                  return(-1);
# Line 113 | Line 173 | register FILE  *fp;
173                  if ((code = getc(fp)) == EOF)
174                      return(-1);
175                  if (code > 128) {       /* run */
176 <                    scanline[j++][i] = getc(fp);
177 <                    for (code &= 127; --code; j++)
178 <                        scanline[j][i] = scanline[j-1][i];
179 <                } else                  /* non-run */
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] = getc(fp);
183 <            }
184 <        return(feof(fp) ? -1 : 0);
185 < }
186 <
187 <
188 < oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
189 < register COLR  *scanline;
190 < int  len;
130 < register FILE  *fp;
131 < {
132 <        int  rshift;
133 <        register int  i;
134 <        
135 <        rshift = 0;
136 <        
137 <        while (len > 0) {
138 <                scanline[0][RED] = getc(fp);
139 <                scanline[0][GRN] = getc(fp);
140 <                scanline[0][BLU] = getc(fp);
141 <                scanline[0][EXP] = getc(fp);
142 <                if (feof(fp) || ferror(fp))
143 <                        return(-1);
144 <                if (scanline[0][RED] == 1 &&
145 <                                scanline[0][GRN] == 1 &&
146 <                                scanline[0][BLU] == 1) {
147 <                        for (i = scanline[0][EXP] << rshift; i > 0; i--) {
148 <                                copycolr(scanline[0], scanline[-1]);
149 <                                scanline++;
150 <                                len--;
151 <                        }
152 <                        rshift += 8;
153 <                } else {
154 <                        scanline++;
155 <                        len--;
156 <                        rshift = 0;
182 >                        scanline[j++][i] = val;
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 <        }
192 >            }
193          return(0);
194   }
195  
196  
197 < fwritescan(scanline, len, fp)           /* write out a scanline */
198 < register COLOR  *scanline;
199 < int  len;
200 < FILE  *fp;
197 > int
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 185 | Line 221 | FILE  *fp;
221   }
222  
223  
224 < freadscan(scanline, len, fp)            /* read in a scanline */
225 < register COLOR  *scanline;
226 < int  len;
227 < FILE  *fp;
224 > int
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 200 | 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 212 | Line 250 | FILE  *fp;
250   }
251  
252  
253 < setcolr(clr, r, g, b)           /* assign a short color value */
254 < register COLR  clr;
255 < double  r, g, b;
253 > void
254 > setcolr(                        /* assign a short color value */
255 >        COLR  clr,
256 >        double  r,
257 >        double  g,
258 >        double  b
259 > )
260   {
219        double  frexp();
261          double  d;
262          int  e;
263          
# Line 229 | Line 270 | double  r, g, b;
270                  return;
271          }
272  
273 <        d = frexp(d, &e) * 256.0 / d;
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 < colr_color(col, clr)            /* convert short to float color */
293 < register COLOR  col;
294 < register COLR  clr;
292 > void
293 > colr_color(                     /* convert short to float color */
294 >        COLOR  col,
295 >        COLR  clr
296 > )
297   {
298          double  f;
299          
# Line 255 | Line 308 | register COLR  clr;
308   }
309  
310  
311 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
312 < register COLOR  c1, c2;
313 < double  md;
311 > int
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