ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/color.c
Revision: 2.19
Committed: Tue Apr 10 23:42:52 2018 UTC (6 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.18: +3 -2 lines
Log Message:
Forgot one #undef

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: color.c,v 2.18 2018/04/10 23:38:40 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 > 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;
45 }
46 return(tempbuf);
47 }
48
49
50 int
51 fwritecolrs( /* write out a colr scanline */
52 COLR *scanline,
53 int len,
54 FILE *fp
55 )
56 {
57 int i, j, beg, cnt = 1;
58 int c2;
59
60 if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */
61 return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
62 /* put magic header */
63 putc(2, fp);
64 putc(2, fp);
65 putc(len>>8, fp);
66 putc(len&255, fp);
67 /* put components seperately */
68 for (i = 0; i < 4; i++) {
69 for (j = 0; j < len; j += cnt) { /* find next run */
70 for (beg = j; beg < len; beg += cnt) {
71 for (cnt = 1; cnt < 127 && beg+cnt < len &&
72 scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
73 ;
74 if (cnt >= MINRUN)
75 break; /* long enough */
76 }
77 if (beg-j > 1 && beg-j < MINRUN) {
78 c2 = j+1;
79 while (scanline[c2++][i] == scanline[j][i])
80 if (c2 == beg) { /* short run */
81 putc(128+beg-j, fp);
82 putc(scanline[j][i], fp);
83 j = beg;
84 break;
85 }
86 }
87 while (j < beg) { /* write out non-run */
88 if ((c2 = beg-j) > 128) c2 = 128;
89 putc(c2, fp);
90 while (c2--)
91 putc(scanline[j++][i], fp);
92 }
93 if (cnt >= MINRUN) { /* write out run */
94 putc(128+cnt, fp);
95 putc(scanline[beg][i], fp);
96 } else
97 cnt = 0;
98 }
99 }
100 return(ferror(fp) ? -1 : 0);
101 }
102
103
104 static int
105 oldreadcolrs( /* read in an old-style colr scanline */
106 COLR *scanline,
107 int len,
108 FILE *fp
109 )
110 {
111 int rshift = 0;
112 int i;
113
114 while (len > 0) {
115 scanline[0][RED] = getc(fp);
116 scanline[0][GRN] = getc(fp);
117 scanline[0][BLU] = getc(fp);
118 scanline[0][EXP] = i = getc(fp);
119 if (i == EOF)
120 return(-1);
121 if (scanline[0][RED] == 1 &&
122 scanline[0][GRN] == 1 &&
123 scanline[0][BLU] == 1) {
124 for (i = scanline[0][EXP] << rshift; i > 0; i--) {
125 copycolr(scanline[0], scanline[-1]);
126 scanline++;
127 len--;
128 }
129 rshift += 8;
130 } else {
131 scanline++;
132 len--;
133 rshift = 0;
134 }
135 }
136 return(0);
137 }
138
139
140 int
141 freadcolrs( /* read in an encoded colr scanline */
142 COLR *scanline,
143 int len,
144 FILE *fp
145 )
146 {
147 int i, j;
148 int code, val;
149 /* determine scanline type */
150 if ((len < MINELEN) | (len > MAXELEN))
151 return(oldreadcolrs(scanline, len, fp));
152 if ((i = getc(fp)) == EOF)
153 return(-1);
154 if (i != 2) {
155 ungetc(i, fp);
156 return(oldreadcolrs(scanline, len, fp));
157 }
158 scanline[0][GRN] = getc(fp);
159 scanline[0][BLU] = getc(fp);
160 if ((i = getc(fp)) == EOF)
161 return(-1);
162 if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
163 scanline[0][RED] = 2;
164 scanline[0][EXP] = i;
165 return(oldreadcolrs(scanline+1, len-1, fp));
166 }
167 if ((scanline[0][BLU]<<8 | i) != len)
168 return(-1); /* length mismatch! */
169 /* read each component */
170 for (i = 0; i < 4; i++)
171 for (j = 0; j < len; ) {
172 if ((code = getc(fp)) == EOF)
173 return(-1);
174 if (code > 128) { /* run */
175 code &= 127;
176 if ((val = getc(fp)) == EOF)
177 return -1;
178 if (j + code > len)
179 return -1; /* overrun */
180 while (code--)
181 scanline[j++][i] = val;
182 } else { /* non-run */
183 if (j + code > len)
184 return -1; /* overrun */
185 while (code--) {
186 if ((val = getc(fp)) == EOF)
187 return -1;
188 scanline[j++][i] = val;
189 }
190 }
191 }
192 return(0);
193 }
194
195
196 int
197 fwritescan( /* write out a scanline */
198 COLOR *scanline,
199 int len,
200 FILE *fp
201 )
202 {
203 COLR *clrscan;
204 int n;
205 COLR *sp;
206 /* get scanline buffer */
207 if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
208 return(-1);
209 clrscan = sp;
210 /* convert scanline */
211 n = len;
212 while (n-- > 0) {
213 setcolr(sp[0], scanline[0][RED],
214 scanline[0][GRN],
215 scanline[0][BLU]);
216 scanline++;
217 sp++;
218 }
219 return(fwritecolrs(clrscan, len, fp));
220 }
221
222
223 int
224 freadscan( /* read in a scanline */
225 COLOR *scanline,
226 int len,
227 FILE *fp
228 )
229 {
230 COLR *clrscan;
231
232 if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
233 return(-1);
234 if (freadcolrs(clrscan, len, fp) < 0)
235 return(-1);
236 /* convert scanline */
237 colr_color(scanline[0], clrscan[0]);
238 while (--len > 0) {
239 scanline++; clrscan++;
240 if (clrscan[0][RED] == clrscan[-1][RED] &&
241 clrscan[0][GRN] == clrscan[-1][GRN] &&
242 clrscan[0][BLU] == clrscan[-1][BLU] &&
243 clrscan[0][EXP] == clrscan[-1][EXP])
244 copycolor(scanline[0], scanline[-1]);
245 else
246 colr_color(scanline[0], clrscan[0]);
247 }
248 return(0);
249 }
250
251
252 void
253 setcolr( /* assign a short color value */
254 COLR clr,
255 double r,
256 double g,
257 double b
258 )
259 {
260 double d;
261 int e;
262
263 d = r > g ? r : g;
264 if (b > d) d = b;
265
266 if (d <= 1e-32) {
267 clr[RED] = clr[GRN] = clr[BLU] = 0;
268 clr[EXP] = 0;
269 return;
270 }
271
272 d = frexp(d, &e) * 255.9999 / d;
273
274 if (r > 0.0)
275 clr[RED] = r * d;
276 else
277 clr[RED] = 0;
278 if (g > 0.0)
279 clr[GRN] = g * d;
280 else
281 clr[GRN] = 0;
282 if (b > 0.0)
283 clr[BLU] = b * d;
284 else
285 clr[BLU] = 0;
286
287 clr[EXP] = e + COLXS;
288 }
289
290
291 void
292 colr_color( /* convert short to float color */
293 COLOR col,
294 COLR clr
295 )
296 {
297 double f;
298
299 if (clr[EXP] == 0)
300 col[RED] = col[GRN] = col[BLU] = 0.0;
301 else {
302 f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
303 col[RED] = (clr[RED] + 0.5)*f;
304 col[GRN] = (clr[GRN] + 0.5)*f;
305 col[BLU] = (clr[BLU] + 0.5)*f;
306 }
307 }
308
309
310 int
311 bigdiff( /* c1 delta c2 > md? */
312 COLOR c1,
313 COLOR c2,
314 double md
315 )
316 {
317 int i;
318
319 for (i = 0; i < 3; i++)
320 if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
321 colval(c2,i)-colval(c1,i) > md*colval(c1,i))
322 return(1);
323 return(0);
324 }