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.13 by greg, Fri Oct 19 11:13:02 1990 UTC vs.
Revision 2.18 by greg, Tue Apr 10 23:38:40 2018 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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 + #define getc    getc_unlocked
21 + #define putc    putc_unlocked
22 + #define ferror  ferror_unlocked
23 + #endif
24  
25 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
26 < register COLR  *scanline;
27 < int  len;
28 < register FILE  *fp;
25 > #define  MINELEN        8       /* minimum scanline length for encoding */
26 > #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
27 > #define  MINRUN         4       /* minimum run length */
28 >
29 >
30 > char *
31 > tempbuffer(                     /* get a temporary buffer */
32 >        unsigned int  len
33 > )
34   {
35 <        COLR  lastcolr;
36 <        int  rept;
35 >        static char  *tempbuf = NULL;
36 >        static unsigned  tempbuflen = 0;
37 >
38 >        if (len > tempbuflen) {
39 >                if (tempbuflen > 0)
40 >                        tempbuf = (char *)realloc((void *)tempbuf, len);
41 >                else
42 >                        tempbuf = (char *)malloc(len);
43 >                tempbuflen = tempbuf==NULL ? 0 : len;
44 >        }
45 >        return(tempbuf);
46 > }
47 >
48 >
49 > int
50 > fwritecolrs(                    /* write out a colr scanline */
51 >        COLR  *scanline,
52 >        int  len,
53 >        FILE  *fp
54 > )
55 > {
56 >        int  i, j, beg, cnt = 1;
57 >        int  c2;
58          
59 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
60 <        lastcolr[EXP] = 0;
61 <        rept = 0;
62 <        
63 <        while (len > 0) {
64 <                if (scanline[0][EXP] == lastcolr[EXP] &&
65 <                                scanline[0][RED] == lastcolr[RED] &&
66 <                                scanline[0][GRN] == lastcolr[GRN] &&
67 <                                scanline[0][BLU] == lastcolr[BLU])
68 <                        rept++;
69 <                else {
70 <                        while (rept) {          /* write out count */
71 <                                putc(1, fp);
72 <                                putc(1, fp);
73 <                                putc(1, fp);
74 <                                putc(rept & 255, fp);
75 <                                rept >>= 8;
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);
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 &&
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) {
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                          }
44                        putc(scanline[0][RED], fp);     /* new color */
45                        putc(scanline[0][GRN], fp);
46                        putc(scanline[0][BLU], fp);
47                        putc(scanline[0][EXP], fp);
48                        copycolr(lastcolr, scanline[0]);
49                        rept = 0;
85                  }
86 <                scanline++;
87 <                len--;
86 >                while (j < beg) {               /* write out non-run */
87 >                    if ((c2 = beg-j) > 128) c2 = 128;
88 >                    putc(c2, fp);
89 >                    while (c2--)
90 >                        putc(scanline[j++][i], fp);
91 >                }
92 >                if (cnt >= MINRUN) {            /* write out run */
93 >                    putc(128+cnt, fp);
94 >                    putc(scanline[beg][i], fp);
95 >                } else
96 >                    cnt = 0;
97 >            }
98          }
54        while (rept) {          /* write out count */
55                putc(1, fp);
56                putc(1, fp);
57                putc(1, fp);
58                putc(rept & 255, fp);
59                rept >>= 8;
60        }
99          return(ferror(fp) ? -1 : 0);
100   }
101  
102  
103 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
104 < register COLR  *scanline;
105 < int  len;
106 < register FILE  *fp;
103 > static int
104 > oldreadcolrs(                   /* read in an old 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          
73        rshift = 0;
74        
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 &&
# Line 98 | Line 136 | register FILE  *fp;
136   }
137  
138  
139 < fwritescan(scanline, len, fp)           /* write out a scanline */
140 < register COLOR  *scanline;
141 < int  len;
142 < register FILE  *fp;
139 > int
140 > freadcolrs(                     /* read in an encoded colr scanline */
141 >        COLR  *scanline,
142 >        int  len,
143 >        FILE  *fp
144 > )
145   {
146 <        COLR  lastcolr, thiscolr;
147 <        int  rept;
148 <        
149 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
150 <        lastcolr[EXP] = 0;
151 <        rept = 0;
152 <        
153 <        while (len > 0) {
154 <                setcolr(thiscolr, scanline[0][RED],
146 >        int  i, j;
147 >        int  code, val;
148 >                                        /* determine scanline type */
149 >        if ((len < MINELEN) | (len > MAXELEN))
150 >                return(oldreadcolrs(scanline, len, fp));
151 >        if ((i = getc(fp)) == EOF)
152 >                return(-1);
153 >        if (i != 2) {
154 >                ungetc(i, fp);
155 >                return(oldreadcolrs(scanline, len, fp));
156 >        }
157 >        scanline[0][GRN] = getc(fp);
158 >        scanline[0][BLU] = getc(fp);
159 >        if ((i = getc(fp)) == EOF)
160 >                return(-1);
161 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
162 >                scanline[0][RED] = 2;
163 >                scanline[0][EXP] = i;
164 >                return(oldreadcolrs(scanline+1, len-1, fp));
165 >        }
166 >        if ((scanline[0][BLU]<<8 | i) != len)
167 >                return(-1);             /* length mismatch! */
168 >                                        /* read each component */
169 >        for (i = 0; i < 4; i++)
170 >            for (j = 0; j < len; ) {
171 >                if ((code = getc(fp)) == EOF)
172 >                    return(-1);
173 >                if (code > 128) {       /* run */
174 >                    code &= 127;
175 >                    if ((val = getc(fp)) == EOF)
176 >                        return -1;
177 >                    if (j + code > len)
178 >                        return -1;      /* overrun */
179 >                    while (code--)
180 >                        scanline[j++][i] = val;
181 >                } else {                /* non-run */
182 >                    if (j + code > len)
183 >                        return -1;      /* overrun */
184 >                    while (code--) {
185 >                        if ((val = getc(fp)) == EOF)
186 >                            return -1;
187 >                        scanline[j++][i] = val;
188 >                    }
189 >                }
190 >            }
191 >        return(0);
192 > }
193 >
194 >
195 > int
196 > fwritescan(                     /* write out a scanline */
197 >        COLOR  *scanline,
198 >        int  len,
199 >        FILE  *fp
200 > )
201 > {
202 >        COLR  *clrscan;
203 >        int  n;
204 >        COLR  *sp;
205 >                                        /* get scanline buffer */
206 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
207 >                return(-1);
208 >        clrscan = sp;
209 >                                        /* convert scanline */
210 >        n = len;
211 >        while (n-- > 0) {
212 >                setcolr(sp[0], scanline[0][RED],
213                                    scanline[0][GRN],
214                                    scanline[0][BLU]);
117                if (thiscolr[EXP] == lastcolr[EXP] &&
118                                thiscolr[RED] == lastcolr[RED] &&
119                                thiscolr[GRN] == lastcolr[GRN] &&
120                                thiscolr[BLU] == lastcolr[BLU])
121                        rept++;
122                else {
123                        while (rept) {          /* write out count */
124                                putc(1, fp);
125                                putc(1, fp);
126                                putc(1, fp);
127                                putc(rept & 255, fp);
128                                rept >>= 8;
129                        }
130                        putc(thiscolr[RED], fp);        /* new color */
131                        putc(thiscolr[GRN], fp);
132                        putc(thiscolr[BLU], fp);
133                        putc(thiscolr[EXP], fp);
134                        copycolr(lastcolr, thiscolr);
135                        rept = 0;
136                }
215                  scanline++;
216 <                len--;
216 >                sp++;
217          }
218 <        while (rept) {          /* write out count */
141 <                putc(1, fp);
142 <                putc(1, fp);
143 <                putc(1, fp);
144 <                putc(rept & 255, fp);
145 <                rept >>= 8;
146 <        }
147 <        return(ferror(fp) ? -1 : 0);
218 >        return(fwritecolrs(clrscan, len, fp));
219   }
220  
221  
222 < freadscan(scanline, len, fp)            /* read in a scanline */
223 < register COLOR  *scanline;
224 < int  len;
225 < register FILE  *fp;
222 > int
223 > freadscan(                      /* read in a scanline */
224 >        COLOR  *scanline,
225 >        int  len,
226 >        FILE  *fp
227 > )
228   {
229 <        COLR  thiscolr;
230 <        int  rshift;
231 <        register int  i;
232 <        
233 <        rshift = 0;
234 <        
235 <        while (len > 0) {
236 <                thiscolr[RED] = getc(fp);
237 <                thiscolr[GRN] = getc(fp);
238 <                thiscolr[BLU] = getc(fp);
239 <                thiscolr[EXP] = getc(fp);
240 <                if (feof(fp) || ferror(fp))
241 <                        return(-1);
242 <                if (thiscolr[RED] == 1 &&
243 <                                thiscolr[GRN] == 1 &&
244 <                                thiscolr[BLU] == 1) {
245 <                        for (i = thiscolr[EXP] << rshift; i > 0; i--) {
173 <                                copycolor(scanline[0], scanline[-1]);
174 <                                scanline++;
175 <                                len--;
176 <                        }
177 <                        rshift += 8;
178 <                } else {
179 <                        colr_color(scanline[0], thiscolr);
180 <                        scanline++;
181 <                        len--;
182 <                        rshift = 0;
183 <                }
229 >        COLR  *clrscan;
230 >
231 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
232 >                return(-1);
233 >        if (freadcolrs(clrscan, len, fp) < 0)
234 >                return(-1);
235 >                                        /* convert scanline */
236 >        colr_color(scanline[0], clrscan[0]);
237 >        while (--len > 0) {
238 >                scanline++; clrscan++;
239 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
240 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
241 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
242 >                            clrscan[0][EXP] == clrscan[-1][EXP])
243 >                        copycolor(scanline[0], scanline[-1]);
244 >                else
245 >                        colr_color(scanline[0], clrscan[0]);
246          }
247          return(0);
248   }
249  
250  
251 < setcolr(clr, r, g, b)           /* assign a short color value */
252 < register COLR  clr;
253 < double  r, g, b;
251 > void
252 > setcolr(                        /* assign a short color value */
253 >        COLR  clr,
254 >        double  r,
255 >        double  g,
256 >        double  b
257 > )
258   {
193        double  frexp();
259          double  d;
260          int  e;
261          
# Line 203 | Line 268 | double  r, g, b;
268                  return;
269          }
270  
271 <        d = frexp(d, &e) * 256.0 / d;
271 >        d = frexp(d, &e) * 255.9999 / d;
272  
273 <        clr[RED] = r * d;
274 <        clr[GRN] = g * d;
275 <        clr[BLU] = b * d;
273 >        if (r > 0.0)
274 >                clr[RED] = r * d;
275 >        else
276 >                clr[RED] = 0;
277 >        if (g > 0.0)
278 >                clr[GRN] = g * d;
279 >        else
280 >                clr[GRN] = 0;
281 >        if (b > 0.0)
282 >                clr[BLU] = b * d;
283 >        else
284 >                clr[BLU] = 0;
285 >
286          clr[EXP] = e + COLXS;
287   }
288  
289  
290 < colr_color(col, clr)            /* convert short to float color */
291 < register COLOR  col;
292 < register COLR  clr;
290 > void
291 > colr_color(                     /* convert short to float color */
292 >        COLOR  col,
293 >        COLR  clr
294 > )
295   {
296          double  f;
297          
# Line 229 | Line 306 | register COLR  clr;
306   }
307  
308  
309 < bigdiff(c1, c2, md)                     /* c1 delta c2 > md? */
310 < register COLOR  c1, c2;
311 < double  md;
309 > int
310 > bigdiff(                                /* c1 delta c2 > md? */
311 >        COLOR  c1,
312 >        COLOR  c2,
313 >        double  md
314 > )
315   {
316 <        register int  i;
316 >        int  i;
317  
318          for (i = 0; i < 3; i++)
319                  if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines