ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/color.c
Revision: 2.21
Committed: Mon May 27 15:44:26 2019 UTC (4 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.20: +4 -4 lines
Log Message:
Fixed potential bug where RGB=(0.01953125 0.01953125 1)*2^N for any N could encode as an RLE start sequence

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: color.c,v 2.20 2019/02/23 19:11:25 greg Exp $";
3 #endif
4 /*
5 * color.c - routines for color calculations.
6 *
7 * Externals declared in color.h
8 */
9
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( /* get a temporary buffer */
33 unsigned int len
34 )
35 {
36 static char *tempbuf = NULL;
37 static unsigned tempbuflen = 0;
38
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 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 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&0xff, 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 }
85 }
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 }
99 return(ferror(fp) ? -1 : 0);
100 }
101
102
103 static int
104 oldreadcolrs( /* read in an old-style colr scanline */
105 COLR *scanline,
106 int len,
107 FILE *fp
108 )
109 {
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) | (len > MAXELEN))
152 return(oldreadcolrs(scanline, len, fp));
153 if ((i = getc(fp)) == EOF)
154 return(-1);
155 if (i != 2) {
156 ungetc(i, fp);
157 return(oldreadcolrs(scanline, len, fp));
158 }
159 scanline[0][GRN] = getc(fp);
160 scanline[0][BLU] = getc(fp);
161 if ((i = getc(fp)) == EOF)
162 return(-1);
163 if ((scanline[0][GRN] != 2) | ((scanline[0][BLU] & 0x80) != 0)) {
164 scanline[0][RED] = 2;
165 scanline[0][EXP] = i;
166 return(oldreadcolrs(scanline+1, len-1, fp));
167 }
168 if ((scanline[0][BLU]<<8 | i) != len)
169 return(-1); /* length mismatch! */
170 /* read each component */
171 for (i = 0; i < 4; i++)
172 for (j = 0; j < len; ) {
173 if ((code = getc(fp)) == EOF)
174 return(-1);
175 if (code > 128) { /* 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] = 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 }
193 return(0);
194 }
195
196
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 COLR *sp;
207 /* get scanline buffer */
208 if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
209 return(-1);
210 clrscan = sp;
211 /* convert scanline */
212 n = len;
213 while (n-- > 0) {
214 setcolr(sp[0], scanline[0][RED],
215 scanline[0][GRN],
216 scanline[0][BLU]);
217 scanline++;
218 sp++;
219 }
220 return(fwritecolrs(clrscan, len, fp));
221 }
222
223
224 int
225 freadscan( /* read in a scanline */
226 COLOR *scanline,
227 int len,
228 FILE *fp
229 )
230 {
231 COLR *clrscan;
232
233 if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
234 return(-1);
235 if (freadcolrs(clrscan, len, fp) < 0)
236 return(-1);
237 /* convert scanline */
238 colr_color(scanline[0], clrscan[0]);
239 while (--len > 0) {
240 scanline++; clrscan++;
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]);
248 }
249 return(0);
250 }
251
252
253 void
254 setcolr( /* assign a short color value */
255 COLR clr,
256 double r,
257 double g,
258 double b
259 )
260 {
261 double d;
262 int e;
263
264 d = r > g ? r : g;
265 if (b > d) d = b;
266
267 if (d <= 1e-32) {
268 clr[RED] = clr[GRN] = clr[BLU] = 0;
269 clr[EXP] = 0;
270 return;
271 }
272
273 d = frexp(d, &e) * 256.0 / d;
274
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 void
293 colr_color( /* convert short to float color */
294 COLOR col,
295 COLR clr
296 )
297 {
298 double f;
299
300 if (clr[EXP] == 0)
301 col[RED] = col[GRN] = col[BLU] = 0.0;
302 else {
303 f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
304 col[RED] = (clr[RED] + 0.5)*f;
305 col[GRN] = (clr[GRN] + 0.5)*f;
306 col[BLU] = (clr[BLU] + 0.5)*f;
307 }
308 }
309
310
311 int
312 bigdiff( /* c1 delta c2 > md? */
313 COLOR c1,
314 COLOR c2,
315 double md
316 )
317 {
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))
323 return(1);
324 return(0);
325 }