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 |
+ |
/* ==================================================================== |
11 |
+ |
* The Radiance Software License, Version 1.0 |
12 |
+ |
* |
13 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
+ |
* |
16 |
+ |
* Redistribution and use in source and binary forms, with or without |
17 |
+ |
* modification, are permitted provided that the following conditions |
18 |
+ |
* are met: |
19 |
+ |
* |
20 |
+ |
* 1. Redistributions of source code must retain the above copyright |
21 |
+ |
* notice, this list of conditions and the following disclaimer. |
22 |
+ |
* |
23 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
+ |
* notice, this list of conditions and the following disclaimer in |
25 |
+ |
* the documentation and/or other materials provided with the |
26 |
+ |
* distribution. |
27 |
+ |
* |
28 |
+ |
* 3. The end-user documentation included with the redistribution, |
29 |
+ |
* if any, must include the following acknowledgment: |
30 |
+ |
* "This product includes Radiance software |
31 |
+ |
* (http://radsite.lbl.gov/) |
32 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
33 |
+ |
* (http://www.lbl.gov/)." |
34 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
36 |
+ |
* |
37 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
+ |
* and "The Regents of the University of California" must |
39 |
+ |
* not be used to endorse or promote products derived from this |
40 |
+ |
* software without prior written permission. For written |
41 |
+ |
* permission, please contact [email protected]. |
42 |
+ |
* |
43 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
44 |
+ |
* nor may "Radiance" appear in their name, without prior written |
45 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
46 |
+ |
* |
47 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
+ |
* SUCH DAMAGE. |
59 |
+ |
* ==================================================================== |
60 |
+ |
* |
61 |
+ |
* This software consists of voluntary contributions made by many |
62 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
+ |
* <http://www.lbl.gov/>. |
65 |
+ |
*/ |
66 |
+ |
|
67 |
|
#include <stdio.h> |
68 |
|
|
69 |
+ |
#include <stdlib.h> |
70 |
+ |
|
71 |
+ |
#include <math.h> |
72 |
+ |
|
73 |
|
#include "color.h" |
74 |
|
|
75 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
76 |
< |
#define MAXELEN 0x7ffe /* maximum scanline length for encoding */ |
76 |
> |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
77 |
|
#define MINRUN 4 /* minimum run length */ |
78 |
|
|
21 |
– |
#ifndef frexp |
22 |
– |
extern double frexp(); |
23 |
– |
#endif |
79 |
|
|
25 |
– |
|
80 |
|
char * |
81 |
|
tempbuffer(len) /* get a temporary buffer */ |
82 |
< |
unsigned len; |
82 |
> |
unsigned int len; |
83 |
|
{ |
30 |
– |
extern char *malloc(), *realloc(); |
84 |
|
static char *tempbuf = NULL; |
85 |
|
static unsigned tempbuflen = 0; |
86 |
|
|
87 |
|
if (len > tempbuflen) { |
88 |
|
if (tempbuflen > 0) |
89 |
< |
tempbuf = realloc(tempbuf, len); |
89 |
> |
tempbuf = (char *)realloc(tempbuf, len); |
90 |
|
else |
91 |
< |
tempbuf = malloc(len); |
91 |
> |
tempbuf = (char *)malloc(len); |
92 |
|
tempbuflen = tempbuf==NULL ? 0 : len; |
93 |
|
} |
94 |
|
return(tempbuf); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
+ |
int |
99 |
|
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
100 |
|
register COLR *scanline; |
101 |
< |
unsigned len; |
101 |
> |
int len; |
102 |
|
register FILE *fp; |
103 |
|
{ |
104 |
< |
register int i, j, beg, cnt; |
104 |
> |
register int i, j, beg, cnt = 1; |
105 |
|
int c2; |
106 |
|
|
107 |
|
if (len < MINELEN | len > MAXELEN) /* OOBs, write out flat */ |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
+ |
static int |
152 |
+ |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
153 |
+ |
register COLR *scanline; |
154 |
+ |
int len; |
155 |
+ |
register FILE *fp; |
156 |
+ |
{ |
157 |
+ |
int rshift; |
158 |
+ |
register int i; |
159 |
+ |
|
160 |
+ |
rshift = 0; |
161 |
+ |
|
162 |
+ |
while (len > 0) { |
163 |
+ |
scanline[0][RED] = getc(fp); |
164 |
+ |
scanline[0][GRN] = getc(fp); |
165 |
+ |
scanline[0][BLU] = getc(fp); |
166 |
+ |
scanline[0][EXP] = getc(fp); |
167 |
+ |
if (feof(fp) || ferror(fp)) |
168 |
+ |
return(-1); |
169 |
+ |
if (scanline[0][RED] == 1 && |
170 |
+ |
scanline[0][GRN] == 1 && |
171 |
+ |
scanline[0][BLU] == 1) { |
172 |
+ |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
173 |
+ |
copycolr(scanline[0], scanline[-1]); |
174 |
+ |
scanline++; |
175 |
+ |
len--; |
176 |
+ |
} |
177 |
+ |
rshift += 8; |
178 |
+ |
} else { |
179 |
+ |
scanline++; |
180 |
+ |
len--; |
181 |
+ |
rshift = 0; |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
return(0); |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
|
188 |
+ |
int |
189 |
|
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */ |
190 |
|
register COLR *scanline; |
191 |
|
int len; |
192 |
|
register FILE *fp; |
193 |
|
{ |
194 |
|
register int i, j; |
195 |
< |
int code; |
195 |
> |
int code, val; |
196 |
|
/* determine scanline type */ |
197 |
|
if (len < MINELEN | len > MAXELEN) |
198 |
|
return(oldreadcolrs(scanline, len, fp)); |
219 |
|
if ((code = getc(fp)) == EOF) |
220 |
|
return(-1); |
221 |
|
if (code > 128) { /* run */ |
222 |
< |
scanline[j++][i] = getc(fp); |
223 |
< |
for (code &= 127; --code; j++) |
224 |
< |
scanline[j][i] = scanline[j-1][i]; |
133 |
< |
} else /* non-run */ |
222 |
> |
code &= 127; |
223 |
> |
if ((val = getc(fp)) == EOF) |
224 |
> |
return -1; |
225 |
|
while (code--) |
226 |
< |
scanline[j++][i] = getc(fp); |
226 |
> |
scanline[j++][i] = val; |
227 |
> |
} else /* non-run */ |
228 |
> |
while (code--) { |
229 |
> |
if ((val = getc(fp)) == EOF) |
230 |
> |
return -1; |
231 |
> |
scanline[j++][i] = val; |
232 |
> |
} |
233 |
|
} |
137 |
– |
return(feof(fp) ? -1 : 0); |
138 |
– |
} |
139 |
– |
|
140 |
– |
|
141 |
– |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */ |
142 |
– |
register COLR *scanline; |
143 |
– |
int len; |
144 |
– |
register FILE *fp; |
145 |
– |
{ |
146 |
– |
int rshift; |
147 |
– |
register int i; |
148 |
– |
|
149 |
– |
rshift = 0; |
150 |
– |
|
151 |
– |
while (len > 0) { |
152 |
– |
scanline[0][RED] = getc(fp); |
153 |
– |
scanline[0][GRN] = getc(fp); |
154 |
– |
scanline[0][BLU] = getc(fp); |
155 |
– |
scanline[0][EXP] = getc(fp); |
156 |
– |
if (feof(fp) || ferror(fp)) |
157 |
– |
return(-1); |
158 |
– |
if (scanline[0][RED] == 1 && |
159 |
– |
scanline[0][GRN] == 1 && |
160 |
– |
scanline[0][BLU] == 1) { |
161 |
– |
for (i = scanline[0][EXP] << rshift; i > 0; i--) { |
162 |
– |
copycolr(scanline[0], scanline[-1]); |
163 |
– |
scanline++; |
164 |
– |
len--; |
165 |
– |
} |
166 |
– |
rshift += 8; |
167 |
– |
} else { |
168 |
– |
scanline++; |
169 |
– |
len--; |
170 |
– |
rshift = 0; |
171 |
– |
} |
172 |
– |
} |
234 |
|
return(0); |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
+ |
int |
239 |
|
fwritescan(scanline, len, fp) /* write out a scanline */ |
240 |
|
register COLOR *scanline; |
241 |
|
int len; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
+ |
int |
265 |
|
freadscan(scanline, len, fp) /* read in a scanline */ |
266 |
|
register COLOR *scanline; |
267 |
|
int len; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
+ |
void |
293 |
|
setcolr(clr, r, g, b) /* assign a short color value */ |
294 |
|
register COLR clr; |
295 |
|
double r, g, b; |
306 |
|
return; |
307 |
|
} |
308 |
|
|
309 |
< |
d = frexp(d, &e) * 256.0 / d; |
309 |
> |
d = frexp(d, &e) * 255.9999 / d; |
310 |
|
|
311 |
|
clr[RED] = r * d; |
312 |
|
clr[GRN] = g * d; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
+ |
void |
319 |
|
colr_color(col, clr) /* convert short to float color */ |
320 |
|
register COLOR col; |
321 |
|
register COLR clr; |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
+ |
int |
337 |
|
bigdiff(c1, c2, md) /* c1 delta c2 > md? */ |
338 |
|
register COLOR c1, c2; |
339 |
|
double md; |