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