1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: lookamb.c,v 2.15 2014/06/19 16:26:55 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* lookamb.c - program to examine ambient components. |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "platform.h" |
11 |
#include "ray.h" |
12 |
#include "ambient.h" |
13 |
#include "resolu.h" |
14 |
|
15 |
|
16 |
int dataonly = 0; |
17 |
int header = 1; |
18 |
int reverse = 0; |
19 |
|
20 |
AMBVAL av; |
21 |
|
22 |
#ifndef OLDAMB |
23 |
|
24 |
|
25 |
static void |
26 |
lookamb( /* load & convert ambient values from a file */ |
27 |
FILE *fp |
28 |
) |
29 |
{ |
30 |
FVECT norm, uvec; |
31 |
|
32 |
while (readambval(&av, fp)) { |
33 |
decodedir(norm, av.ndir); |
34 |
decodedir(uvec, av.udir); |
35 |
if (dataonly) { |
36 |
printf("%f\t%f\t%f\t", av.pos[0], av.pos[1], av.pos[2]); |
37 |
printf("%f\t%f\t%f\t", norm[0], norm[1], norm[2]); |
38 |
printf("%f\t%f\t%f\t", uvec[0], uvec[1], uvec[2]); |
39 |
printf("%d\t%f\t%f\t%f\t", av.lvl, av.weight, |
40 |
av.rad[0], av.rad[1]); |
41 |
printf("%e\t%e\t%e\t", colval(av.val,RED), |
42 |
colval(av.val,GRN), |
43 |
colval(av.val,BLU)); |
44 |
printf("%f\t%f\t", av.gpos[0], av.gpos[1]); |
45 |
printf("%f\t%f\t", av.gdir[0], av.gdir[1]); |
46 |
printf("%u\n", av.corral); |
47 |
} else { |
48 |
printf("Position:\t%f\t%f\t%f\n", av.pos[0], |
49 |
av.pos[1], av.pos[2]); |
50 |
printf("Normal:\t\t%f\t%f\t%f\n", |
51 |
norm[0], norm[1], norm[2]); |
52 |
printf("Uvector:\t%f\t%f\t%f\n", |
53 |
uvec[0], uvec[1], uvec[2]); |
54 |
printf("Lvl,Wt,UVrad:\t%d\t\t%f\t%f\t%f\n", av.lvl, |
55 |
av.weight, av.rad[0], av.rad[1]); |
56 |
printf("Value:\t\t%e\t%e\t%e\n", colval(av.val,RED), |
57 |
colval(av.val,GRN), colval(av.val,BLU)); |
58 |
printf("Pos.Grad:\t%f\t%f\n", av.gpos[0], av.gpos[1]); |
59 |
printf("Dir.Grad:\t%f\t%f\n", av.gdir[0], av.gdir[1]); |
60 |
printf("Corral:\t\t%8X\n\n", av.corral); |
61 |
} |
62 |
if (ferror(stdout)) |
63 |
exit(1); |
64 |
} |
65 |
} |
66 |
|
67 |
|
68 |
static void |
69 |
writamb( /* write binary ambient values to stdout */ |
70 |
FILE *fp |
71 |
) |
72 |
{ |
73 |
FVECT norm; |
74 |
|
75 |
for ( ; ; ) { |
76 |
if (!dataonly) |
77 |
fscanf(fp, "%*s"); |
78 |
if (fscanf(fp, "%f %f %f", |
79 |
&av.pos[0], &av.pos[1], &av.pos[2]) != 3) |
80 |
return; |
81 |
if (!dataonly) |
82 |
fscanf(fp, "%*s"); |
83 |
if (fscanf(fp, FVFORMAT, &norm[0], &norm[1], &norm[2]) != 3) |
84 |
return; |
85 |
av.ndir = encodedir(norm); |
86 |
if (!dataonly) |
87 |
fscanf(fp, "%*s"); |
88 |
if (fscanf(fp, FVFORMAT, &norm[0], &norm[1], &norm[2]) != 3) |
89 |
return; |
90 |
av.udir = encodedir(norm); |
91 |
if (!dataonly) |
92 |
fscanf(fp, "%*s"); |
93 |
if (fscanf(fp, "%hd %f %f %f", &av.lvl, &av.weight, |
94 |
&av.rad[0], &av.rad[1]) != 4) |
95 |
return; |
96 |
if (!dataonly) |
97 |
fscanf(fp, "%*s"); |
98 |
if (fscanf(fp, "%f %f %f", |
99 |
&av.val[RED], &av.val[GRN], &av.val[BLU]) != 3) |
100 |
return; |
101 |
if (!dataonly) |
102 |
fscanf(fp, "%*s"); |
103 |
if (fscanf(fp, "%f %f", &av.gpos[0], &av.gpos[1]) != 2) |
104 |
return; |
105 |
if (!dataonly) |
106 |
fscanf(fp, "%*s"); |
107 |
if (fscanf(fp, "%f %f", &av.gdir[0], &av.gdir[1]) != 2) |
108 |
return; |
109 |
if (dataonly) { |
110 |
if (fscanf(fp, "%u", &av.corral) != 1) |
111 |
return; |
112 |
} else if (fscanf(fp, "%*s %X", &av.corral) != 1) |
113 |
return; |
114 |
av.next = NULL; |
115 |
writambval(&av, stdout); |
116 |
if (ferror(stdout)) |
117 |
exit(1); |
118 |
} |
119 |
} |
120 |
|
121 |
|
122 |
#else /* ! NEWAMB */ |
123 |
|
124 |
|
125 |
static void |
126 |
lookamb( /* get ambient values from a file */ |
127 |
FILE *fp |
128 |
) |
129 |
{ |
130 |
while (readambval(&av, fp)) { |
131 |
if (dataonly) { |
132 |
printf("%f\t%f\t%f\t", av.pos[0], av.pos[1], av.pos[2]); |
133 |
printf("%f\t%f\t%f\t", av.dir[0], av.dir[1], av.dir[2]); |
134 |
printf("%d\t%f\t%f\t", av.lvl, av.weight, av.rad); |
135 |
printf("%e\t%e\t%e\t", colval(av.val,RED), |
136 |
colval(av.val,GRN), |
137 |
colval(av.val,BLU)); |
138 |
printf("%f\t%f\t%f\t", av.gpos[0], |
139 |
av.gpos[1], av.gpos[2]); |
140 |
printf("%f\t%f\t%f\n", av.gdir[0], |
141 |
av.gdir[1], av.gdir[2]); |
142 |
} else { |
143 |
printf("Position:\t%f\t%f\t%f\n", av.pos[0], |
144 |
av.pos[1], av.pos[2]); |
145 |
printf("Direction:\t%f\t%f\t%f\n", av.dir[0], |
146 |
av.dir[1], av.dir[2]); |
147 |
printf("Lvl,Wt,Rad:\t%d\t\t%f\t%f\n", av.lvl, |
148 |
av.weight, av.rad); |
149 |
printf("Value:\t\t%e\t%e\t%e\n", colval(av.val,RED), |
150 |
colval(av.val,GRN), colval(av.val,BLU)); |
151 |
printf("Pos.Grad:\t%f\t%f\t%f\n", av.gpos[0], |
152 |
av.gpos[1], av.gpos[2]); |
153 |
printf("Dir.Grad:\t%f\t%f\t%f\n\n", av.gdir[0], |
154 |
av.gdir[1], av.gdir[2]); |
155 |
} |
156 |
if (ferror(stdout)) |
157 |
exit(1); |
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
static void |
163 |
writamb( /* write binary ambient values */ |
164 |
FILE *fp |
165 |
) |
166 |
{ |
167 |
for ( ; ; ) { |
168 |
if (!dataonly) |
169 |
fscanf(fp, "%*s"); |
170 |
if (fscanf(fp, "%f %f %f", |
171 |
&av.pos[0], &av.pos[1], &av.pos[2]) != 3) |
172 |
return; |
173 |
if (!dataonly) |
174 |
fscanf(fp, "%*s"); |
175 |
if (fscanf(fp, "%f %f %f", |
176 |
&av.dir[0], &av.dir[1], &av.dir[2]) != 3) |
177 |
return; |
178 |
if (!dataonly) |
179 |
fscanf(fp, "%*s"); |
180 |
if (fscanf(fp, "%d %f %f", |
181 |
&av.lvl, &av.weight, &av.rad) != 3) |
182 |
return; |
183 |
if (!dataonly) |
184 |
fscanf(fp, "%*s"); |
185 |
if (fscanf(fp, "%f %f %f", |
186 |
&av.val[RED], &av.val[GRN], &av.val[BLU]) != 3) |
187 |
return; |
188 |
if (!dataonly) |
189 |
fscanf(fp, "%*s"); |
190 |
if (fscanf(fp, "%f %f %f", |
191 |
&av.gpos[0], &av.gpos[1], &av.gpos[2]) != 3) |
192 |
return; |
193 |
if (!dataonly) |
194 |
fscanf(fp, "%*s"); |
195 |
if (fscanf(fp, "%f %f %f", |
196 |
&av.gdir[0], &av.gdir[1], &av.gdir[2]) != 3) |
197 |
return; |
198 |
av.next = NULL; |
199 |
writambval(&av, stdout); |
200 |
if (ferror(stdout)) |
201 |
exit(1); |
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
#endif /* ! NEWAMB */ |
207 |
|
208 |
|
209 |
int |
210 |
main( /* load ambient values from a file */ |
211 |
int argc, |
212 |
char *argv[] |
213 |
) |
214 |
{ |
215 |
FILE *fp; |
216 |
int i; |
217 |
|
218 |
for (i = 1; i < argc; i++) |
219 |
if (argv[i][0] == '-') |
220 |
switch (argv[i][1]) { |
221 |
case 'd': |
222 |
dataonly = 1; |
223 |
break; |
224 |
case 'r': |
225 |
reverse = 1; |
226 |
break; |
227 |
case 'h': |
228 |
header = 0; |
229 |
break; |
230 |
default: |
231 |
fprintf(stderr, "%s: unknown option '%s'\n", |
232 |
argv[0], argv[i]); |
233 |
return(1); |
234 |
} |
235 |
else |
236 |
break; |
237 |
|
238 |
if (i >= argc) |
239 |
fp = stdin; |
240 |
else if ((fp = fopen(argv[i], "r")) == NULL) { |
241 |
fprintf(stderr, "%s: file not found\n", argv[i]); |
242 |
return(1); |
243 |
} |
244 |
if (reverse) { |
245 |
if (header) { |
246 |
if (checkheader(fp, "ascii", stdout) < 0) |
247 |
goto formaterr; |
248 |
} else { |
249 |
newheader("RADIANCE", stdout); |
250 |
printargs(argc, argv, stdout); |
251 |
} |
252 |
fputformat(AMBFMT, stdout); |
253 |
putchar('\n'); |
254 |
SET_FILE_BINARY(stdout); |
255 |
putambmagic(stdout); |
256 |
writamb(fp); |
257 |
} else { |
258 |
SET_FILE_BINARY(fp); |
259 |
if (checkheader(fp, AMBFMT, header ? stdout : (FILE *)NULL) < 0) |
260 |
goto formaterr; |
261 |
if (!hasambmagic(fp)) |
262 |
goto formaterr; |
263 |
if (header) { |
264 |
fputformat("ascii", stdout); |
265 |
putchar('\n'); |
266 |
} |
267 |
lookamb(fp); |
268 |
} |
269 |
fclose(fp); |
270 |
return(0); |
271 |
formaterr: |
272 |
fprintf(stderr, "%s: format error on input\n", argv[0]); |
273 |
exit(1); |
274 |
} |