1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.7 |
static const char RCSid[] = "$Id: lampcolor.c,v 2.6 2003/02/22 02:07:23 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
1.2 |
* Program to convert lamp color from table and compute radiance. |
6 |
greg |
1.1 |
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
schorsch |
2.7 |
#include <string.h> |
10 |
greg |
2.4 |
#include <math.h> |
11 |
|
|
|
12 |
schorsch |
2.7 |
#include "standard.h" |
13 |
greg |
1.8 |
#include "color.h" |
14 |
|
|
|
15 |
greg |
1.2 |
#define PI 3.14159265358979323846 |
16 |
|
|
|
17 |
|
|
/* lamp parameters */ |
18 |
|
|
#define LTYPE 0 |
19 |
|
|
#define LUNIT 1 |
20 |
|
|
#define LGEOM 2 |
21 |
|
|
#define LOUTP 3 |
22 |
|
|
#define NPARAMS 4 |
23 |
greg |
1.1 |
|
24 |
schorsch |
2.7 |
static int typecheck(char *s); |
25 |
|
|
static int unitcheck(char *s); |
26 |
|
|
static int geomcheck(char *s); |
27 |
|
|
static int outpcheck(char *s); |
28 |
|
|
static void compute(void); |
29 |
|
|
static int getpolygon(void), getsphere(void), getcylinder(void), getring(void); |
30 |
|
|
|
31 |
greg |
1.2 |
|
32 |
|
|
float *lampcolor; /* the lamp color (RGB) */ |
33 |
|
|
double unit2meter; /* conversion from units to meters */ |
34 |
greg |
1.7 |
double area; /* radiating area for this geometry */ |
35 |
greg |
1.2 |
double lumens; /* total lamp lumens */ |
36 |
|
|
|
37 |
|
|
struct { |
38 |
|
|
char *name; |
39 |
|
|
char value[64]; |
40 |
|
|
int (*check)(); |
41 |
|
|
char *help; |
42 |
|
|
} param[NPARAMS] = { |
43 |
saba |
2.5 |
{ "lamp type", "WHITE", typecheck, |
44 |
greg |
1.2 |
"The lamp type is a string which corresponds to one of the types registered\n\ |
45 |
saba |
2.5 |
in the lamp table file. A value of \"WHITE\" means an uncolored source,\n\ |
46 |
greg |
1.2 |
which may be preferable because it results in a color balanced image." }, |
47 |
|
|
{ "length unit", "meter", unitcheck, |
48 |
|
|
"Unit must be one of: \"meter\", \"centimeter\", \"foot\", or \"inch\".\n\ |
49 |
|
|
These may be abbreviated as a single letter." }, |
50 |
|
|
{ "lamp geometry", "polygon", geomcheck, |
51 |
|
|
"The lamp geometry must be one of: \"polygon\", \"sphere\", \"cylinder\"\n\ |
52 |
|
|
or \"ring\". These may be abbreviated as a single letter." }, |
53 |
|
|
{ "total lamp lumens", "0", outpcheck, |
54 |
|
|
"This is the overall light output of the lamp and its fixture. If you do\n\ |
55 |
|
|
not know this value explicitly, you can compute the approximate lumens\n\ |
56 |
greg |
1.3 |
by multiplying the input wattage by 14 for incandescent fixtures or 70\n\ |
57 |
greg |
1.2 |
for fluorescent fixtures." }, |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
|
|
61 |
greg |
1.1 |
main(argc, argv) |
62 |
|
|
int argc; |
63 |
|
|
char *argv[]; |
64 |
|
|
{ |
65 |
|
|
char *lamptab = "lamp.tab"; |
66 |
greg |
1.2 |
char buf[64]; |
67 |
|
|
int i; |
68 |
greg |
1.1 |
|
69 |
|
|
if (argc > 1) lamptab = argv[1]; |
70 |
|
|
if (loadlamps(lamptab) == 0) { |
71 |
greg |
1.2 |
fprintf(stderr, "%s: no such lamp table\n", lamptab); |
72 |
greg |
1.1 |
exit(1); |
73 |
|
|
} |
74 |
greg |
1.2 |
printf("Program to compute lamp radiance. Enter '?' for help.\n"); |
75 |
greg |
1.1 |
for ( ; ; ) { |
76 |
greg |
1.2 |
i = 0; |
77 |
|
|
while (i < NPARAMS) { |
78 |
|
|
printf("Enter %s [%s]: ", param[i].name, |
79 |
|
|
param[i].value); |
80 |
|
|
if (gets(buf) == NULL) |
81 |
|
|
exit(0); |
82 |
|
|
if (buf[0] == '?') { |
83 |
|
|
puts(param[i].help); |
84 |
|
|
continue; |
85 |
|
|
} |
86 |
|
|
if (buf[0]) |
87 |
|
|
strcpy(param[i].value, buf); |
88 |
|
|
if (!(*param[i].check)(param[i].value)) { |
89 |
|
|
fprintf(stderr, "%s: bad value for %s\n", |
90 |
|
|
param[i].value, param[i].name); |
91 |
|
|
continue; |
92 |
|
|
} |
93 |
|
|
i++; |
94 |
|
|
} |
95 |
|
|
compute(); |
96 |
greg |
1.1 |
} |
97 |
greg |
1.2 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
schorsch |
2.7 |
static int |
101 |
|
|
typecheck( /* check lamp type */ |
102 |
|
|
char *s |
103 |
|
|
) |
104 |
greg |
1.2 |
{ |
105 |
|
|
lampcolor = matchlamp(s); |
106 |
|
|
return(lampcolor != NULL); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
|
110 |
schorsch |
2.7 |
static int |
111 |
|
|
unitcheck( /* compute conversion to meters */ |
112 |
|
|
char *s |
113 |
|
|
) |
114 |
greg |
1.2 |
{ |
115 |
|
|
int len = strlen(s); |
116 |
|
|
|
117 |
|
|
switch (*s) { |
118 |
|
|
case 'm': |
119 |
|
|
if (strncmp(s, "meters", len)) |
120 |
|
|
return(0); |
121 |
|
|
unit2meter = 1.0; |
122 |
|
|
return(1); |
123 |
|
|
case 'c': |
124 |
|
|
if (strncmp(s, "centimeters", len) && strncmp(s, "cms", len)) |
125 |
|
|
return(0); |
126 |
|
|
unit2meter = 0.01; |
127 |
|
|
return(1); |
128 |
|
|
case 'f': |
129 |
|
|
if (strncmp(s, "foot", len) && strncmp(s, "feet", len)) |
130 |
|
|
return(0); |
131 |
|
|
unit2meter = 0.3048; |
132 |
|
|
return(1); |
133 |
|
|
case 'i': |
134 |
|
|
if (strncmp(s, "inches", len)) |
135 |
|
|
return(0); |
136 |
|
|
unit2meter = 0.0254; |
137 |
|
|
return(1); |
138 |
|
|
} |
139 |
|
|
return(0); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
schorsch |
2.7 |
static int |
144 |
|
|
geomcheck( /* check/set lamp geometry */ |
145 |
|
|
char *s |
146 |
|
|
) |
147 |
greg |
1.2 |
{ |
148 |
|
|
int len = strlen(s); |
149 |
|
|
|
150 |
|
|
switch (*s) { |
151 |
|
|
case 'p': |
152 |
|
|
if (strncmp(s, "polygonal", len)) |
153 |
|
|
return(0); |
154 |
|
|
return(getpolygon()); |
155 |
|
|
case 's': |
156 |
|
|
if (strncmp(s, "sphere", len) && strncmp(s, "spherical", len)) |
157 |
|
|
return(0); |
158 |
|
|
return(getsphere()); |
159 |
|
|
case 'c': |
160 |
|
|
if (strncmp(s,"cylinder",len) && strncmp(s,"cylindrical",len)) |
161 |
|
|
return(0); |
162 |
|
|
return(getcylinder()); |
163 |
|
|
case 'r': |
164 |
|
|
if (strncmp(s, "ring", len) && strncmp(s, "disk", len)) |
165 |
|
|
return(0); |
166 |
|
|
return(getring()); |
167 |
|
|
} |
168 |
|
|
return(0); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
|
172 |
schorsch |
2.7 |
static int |
173 |
|
|
outpcheck( /* check lumen output value */ |
174 |
|
|
register char *s |
175 |
|
|
) |
176 |
greg |
1.2 |
{ |
177 |
|
|
if ((*s < '0' || *s > '9') && *s != '.') |
178 |
|
|
return(0); |
179 |
|
|
lumens = atof(s); |
180 |
|
|
return(1); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
schorsch |
2.7 |
static void |
185 |
|
|
compute(void) /* compute lamp radiance */ |
186 |
greg |
1.2 |
{ |
187 |
|
|
double whiteval; |
188 |
|
|
|
189 |
greg |
1.8 |
whiteval = lumens/area/(WHTEFFICACY*PI); |
190 |
greg |
1.2 |
|
191 |
|
|
printf("Lamp color (RGB) = %f %f %f\n", |
192 |
|
|
lampcolor[0]*whiteval, |
193 |
|
|
lampcolor[1]*whiteval, |
194 |
|
|
lampcolor[2]*whiteval); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
|
198 |
greg |
1.6 |
getd(name, dp, help) /* get a positive double from stdin */ |
199 |
|
|
char *name; |
200 |
greg |
1.2 |
double *dp; |
201 |
greg |
1.6 |
char *help; |
202 |
greg |
1.2 |
{ |
203 |
|
|
char buf[32]; |
204 |
greg |
1.6 |
again: |
205 |
|
|
printf("%s [%g]: ", name, *dp); |
206 |
greg |
1.2 |
if (gets(buf) == NULL) |
207 |
|
|
return(0); |
208 |
greg |
1.6 |
if (buf[0] == '?') { |
209 |
|
|
puts(help); |
210 |
|
|
goto again; |
211 |
|
|
} |
212 |
greg |
1.2 |
if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.') |
213 |
|
|
return(0); |
214 |
|
|
*dp = atof(buf); |
215 |
|
|
return(1); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
|
219 |
schorsch |
2.7 |
static int |
220 |
|
|
getpolygon(void) /* get projected area for a polygon */ |
221 |
greg |
1.2 |
{ |
222 |
greg |
1.9 |
static double parea = 1.0; |
223 |
greg |
1.5 |
|
224 |
greg |
1.9 |
getd("Polygon area", &parea, |
225 |
greg |
1.6 |
"Enter the total radiating area of the polygon."); |
226 |
greg |
1.9 |
area = unit2meter*unit2meter * parea; |
227 |
greg |
1.2 |
return(1); |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
schorsch |
2.7 |
static int |
232 |
|
|
getsphere(void) /* get projected area for a sphere */ |
233 |
greg |
1.2 |
{ |
234 |
greg |
1.5 |
static double radius = 1.0; |
235 |
greg |
1.2 |
|
236 |
greg |
1.6 |
getd("Sphere radius", &radius, |
237 |
|
|
"Enter the distance from the sphere's center to its surface."); |
238 |
greg |
1.7 |
area = 4.*PI*unit2meter*unit2meter * radius*radius; |
239 |
greg |
1.2 |
return(1); |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
|
243 |
schorsch |
2.7 |
static int |
244 |
|
|
getcylinder(void) /* get projected area for a cylinder */ |
245 |
greg |
1.2 |
{ |
246 |
greg |
1.5 |
static double length = 1.0, radius = 0.1; |
247 |
greg |
1.2 |
|
248 |
greg |
1.6 |
getd("Cylinder length", &length, |
249 |
|
|
"Enter the length of the cylinder."); |
250 |
|
|
getd("Cylinder radius", &radius, |
251 |
|
|
"Enter the distance from the cylinder's axis to its surface."); |
252 |
greg |
1.7 |
area = 2.*PI*unit2meter*unit2meter * radius*length; |
253 |
greg |
1.2 |
return(1); |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
|
257 |
schorsch |
2.7 |
static int |
258 |
|
|
getring(void) /* get projected area for a ring */ |
259 |
greg |
1.2 |
{ |
260 |
greg |
1.5 |
static double radius = 1.0; |
261 |
greg |
1.2 |
|
262 |
greg |
1.6 |
getd("Disk radius", &radius, |
263 |
|
|
"Enter the distance from the ring's center to its outer edge.\n\ |
264 |
|
|
The inner radius must be zero."); |
265 |
greg |
1.7 |
area = PI*unit2meter*unit2meter * radius*radius; |
266 |
greg |
1.2 |
return(1); |
267 |
greg |
1.1 |
} |