1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Radiance holodeck picture generator |
6 |
*/ |
7 |
|
8 |
#include "rholo.h" |
9 |
#include "view.h" |
10 |
|
11 |
char *progname; /* our program name */ |
12 |
char *hdkfile; /* holodeck file name */ |
13 |
char gargc; /* global argc */ |
14 |
char **gargv; /* global argv */ |
15 |
|
16 |
VIEW myview = STDVIEW; /* current output view */ |
17 |
int xres = 512, yres = 512; /* max. horizontal and vertical resolution */ |
18 |
char *outspec = NULL; /* output file specification */ |
19 |
double randfrac = -1.; /* random resampling fraction */ |
20 |
double pixaspect = 1.; /* pixel aspect ratio */ |
21 |
int seqstart = 0; /* sequence start frame */ |
22 |
double expval = 1.; /* exposure value */ |
23 |
|
24 |
COLOR *mypixel; /* pixels being rendered */ |
25 |
float *myweight; /* weights (used to compute final pixels) */ |
26 |
float *mydepth; /* depth values (visibility culling) */ |
27 |
int hres, vres; /* current horizontal and vertical res. */ |
28 |
|
29 |
extern int nowarn; /* turn warnings off? */ |
30 |
|
31 |
|
32 |
main(argc, argv) |
33 |
int argc; |
34 |
char *argv[]; |
35 |
{ |
36 |
int i, rval; |
37 |
|
38 |
gargc = argc; gargv = argv; |
39 |
progname = argv[0]; /* get arguments */ |
40 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
41 |
rval = getviewopt(&myview, argc-i, argv+i); |
42 |
if (rval >= 0) { /* view option */ |
43 |
i += rval; |
44 |
continue; |
45 |
} |
46 |
switch (argv[i][1]) { |
47 |
case 'w': /* turn off warnings */ |
48 |
nowarn++; |
49 |
break; |
50 |
case 'p': /* pixel aspect/exposure */ |
51 |
if (badarg(argc-i-1,argv+i+1,"f")) |
52 |
goto userr; |
53 |
if (argv[i][2] == 'a') |
54 |
pixaspect = atof(argv[++i]); |
55 |
else if (argv[i][2] == 'e') { |
56 |
expval = atof(argv[++i]); |
57 |
if (argv[i][0] == '-' | argv[i][0] == '+') |
58 |
expval = pow(2., expval); |
59 |
} else |
60 |
goto userr; |
61 |
break; |
62 |
case 'x': /* horizontal resolution */ |
63 |
if (badarg(argc-i-1,argv+i+1,"i")) |
64 |
goto userr; |
65 |
xres = atoi(argv[++i]); |
66 |
break; |
67 |
case 'y': /* vertical resolution */ |
68 |
if (badarg(argc-i-1,argv+i+1,"i")) |
69 |
goto userr; |
70 |
yres = atoi(argv[++i]); |
71 |
break; |
72 |
case 'o': /* output file specificaiton */ |
73 |
if (badarg(argc-i-1,argv+i+1,"s")) |
74 |
goto userr; |
75 |
outspec = argv[++i]; |
76 |
break; |
77 |
case 'r': /* random sampling */ |
78 |
if (badarg(argc-i-1,argv+i+1,"f")) |
79 |
goto userr; |
80 |
randfrac = atof(argv[++i]); |
81 |
break; |
82 |
case 's': /* smooth sampling */ |
83 |
randfrac = -1.; |
84 |
break; |
85 |
case 'S': /* sequence start */ |
86 |
if (badarg(argc-i-1,argv+i+1,"i")) |
87 |
goto userr; |
88 |
seqstart = atoi(argv[++i]); |
89 |
break; |
90 |
case 'v': /* view file */ |
91 |
if (argv[i][2]!='f' || badarg(argc-i-1,argv+i+1,"s")) |
92 |
goto userr; |
93 |
rval = viewfile(argv[++i], &myview, NULL); |
94 |
if (rval < 0) { |
95 |
sprintf(errmsg, "cannot open view file \"%s\"", |
96 |
argv[i]); |
97 |
error(SYSTEM, errmsg); |
98 |
} else if (rval == 0) { |
99 |
sprintf(errmsg, "bad view file \"%s\"", |
100 |
argv[i]); |
101 |
error(USER, errmsg); |
102 |
} |
103 |
break; |
104 |
default: |
105 |
goto userr; |
106 |
} |
107 |
} |
108 |
/* open holodeck file */ |
109 |
if (i != argc-1) |
110 |
goto userr; |
111 |
hdkfile = argv[i]; |
112 |
initialize(); |
113 |
/* render picture(s) */ |
114 |
if (seqstart <= 0) |
115 |
dopicture(0); |
116 |
else |
117 |
while (nextview(&myview, stdin) != EOF) |
118 |
dopicture(seqstart++); |
119 |
quit(0); /* all done! */ |
120 |
userr: |
121 |
fprintf(stderr, |
122 |
"Usage: %s [-w][-r rf][-pa pa][-pe ex][-x hr][-y vr][-S stfn][-o outp][view] input.hdk\n", |
123 |
progname); |
124 |
quit(1); |
125 |
} |
126 |
|
127 |
|
128 |
dopicture(fn) /* render view from holodeck */ |
129 |
int fn; |
130 |
{ |
131 |
char *err; |
132 |
int rval; |
133 |
BEAMLIST blist; |
134 |
|
135 |
if ((err = setview(&myview)) != NULL) { |
136 |
sprintf(errmsg, "%s -- skipping frame %d", err, fn); |
137 |
error(WARNING, errmsg); |
138 |
return; |
139 |
} |
140 |
startpicture(fn); /* open output picture */ |
141 |
/* determine relevant beams */ |
142 |
viewbeams(&myview, hres, vres, &blist); |
143 |
/* render image */ |
144 |
if (blist.nb > 0) { |
145 |
render_frame(blist.bl, blist.nb); |
146 |
free((void *)blist.bl); |
147 |
} else { |
148 |
sprintf(errmsg, "no section visible in frame %d", fn); |
149 |
error(WARNING, errmsg); |
150 |
} |
151 |
rval = endpicture(); /* write pixel values */ |
152 |
if (rval < 0) { |
153 |
sprintf(errmsg, "error writing frame %d", fn); |
154 |
error(SYSTEM, errmsg); |
155 |
} |
156 |
#ifdef DEBUG |
157 |
if (blist.nb > 0 & rval > 0) { |
158 |
sprintf(errmsg, "%d unrendered pixels in frame %d (%.1f%%)", |
159 |
rval, fn, 100.*rval/(hres*vres)); |
160 |
error(WARNING, errmsg); |
161 |
} |
162 |
#endif |
163 |
} |
164 |
|
165 |
|
166 |
render_frame(bl, nb) /* render frame from beam values */ |
167 |
register PACKHEAD *bl; |
168 |
int nb; |
169 |
{ |
170 |
extern int pixBeam(); |
171 |
register HDBEAMI *bil; |
172 |
register int i; |
173 |
|
174 |
if (nb <= 0) return; |
175 |
if ((bil = (HDBEAMI *)malloc(nb*sizeof(HDBEAMI))) == NULL) |
176 |
error(SYSTEM, "out of memory in render_frame"); |
177 |
for (i = nb; i--; ) { |
178 |
bil[i].h = hdlist[bl[i].hd]; |
179 |
bil[i].b = bl[i].bi; |
180 |
} |
181 |
hdloadbeams(bil, nb, pixBeam); |
182 |
pixFinish(randfrac); |
183 |
free((void *)bil); |
184 |
} |
185 |
|
186 |
|
187 |
startpicture(fn) /* initialize picture for rendering & output */ |
188 |
int fn; |
189 |
{ |
190 |
extern char VersionID[]; |
191 |
double pa = pixaspect; |
192 |
char fname[256]; |
193 |
/* compute picture resolution */ |
194 |
hres = xres; vres = yres; |
195 |
normaspect(viewaspect(&myview), &pa, &hres, &vres); |
196 |
/* prepare output */ |
197 |
if (outspec != NULL) { |
198 |
sprintf(fname, outspec, fn); |
199 |
if (freopen(fname, "w", stdout) == NULL) { |
200 |
sprintf(errmsg, "cannot open output \"%s\"", fname); |
201 |
error(SYSTEM, errmsg); |
202 |
} |
203 |
} |
204 |
/* write header */ |
205 |
newheader("RADIANCE", stdout); |
206 |
printf("SOFTWARE= %s\n", VersionID); |
207 |
printargs(gargc, gargv, stdout); |
208 |
if (fn) |
209 |
printf("FRAME=%d\n", fn); |
210 |
fputs(VIEWSTR, stdout); |
211 |
fprintview(&myview, stdout); |
212 |
fputc('\n', stdout); |
213 |
if (pa < 0.99 | pa > 1.01) |
214 |
fputaspect(pa, stdout); |
215 |
if (expval < 0.99 | expval > 1.01) |
216 |
fputexpos(expval, stdout); |
217 |
fputformat(COLRFMT, stdout); |
218 |
fputc('\n', stdout); |
219 |
/* write resolution (standard order) */ |
220 |
fprtresolu(hres, vres, stdout); |
221 |
/* prepare image buffers */ |
222 |
bzero((char *)mypixel, hres*vres*sizeof(COLOR)); |
223 |
bzero((char *)myweight, hres*vres*sizeof(float)); |
224 |
bzero((char *)mydepth, hres*vres*sizeof(float)); |
225 |
} |
226 |
|
227 |
|
228 |
int |
229 |
endpicture() /* finish and write out pixels */ |
230 |
{ |
231 |
int lastr = -1, nunrend = 0; |
232 |
int4 lastp, lastrp; |
233 |
register int4 p; |
234 |
register double d; |
235 |
/* compute final pixel values */ |
236 |
for (p = hres*vres; p--; ) { |
237 |
if (myweight[p] <= FTINY) { |
238 |
if (lastr >= 0) |
239 |
if (p/hres == lastp/hres) |
240 |
copycolor(mypixel[p], mypixel[lastp]); |
241 |
else |
242 |
copycolor(mypixel[p], mypixel[lastrp]); |
243 |
nunrend++; |
244 |
continue; |
245 |
} |
246 |
d = expval/myweight[p]; |
247 |
scalecolor(mypixel[p], d); |
248 |
if ((lastp=p)/hres != lastr) |
249 |
lastr = (lastrp=p)/hres; |
250 |
} |
251 |
/* write each scanline */ |
252 |
for (p = vres; p--; ) |
253 |
if (fwritescan(mypixel+p*hres, hres, stdout) < 0) |
254 |
return(-1); |
255 |
if (fflush(stdout) == EOF) |
256 |
return(-1); |
257 |
return(nunrend); |
258 |
} |
259 |
|
260 |
|
261 |
initialize() /* initialize holodeck and buffers */ |
262 |
{ |
263 |
extern long ftell(); |
264 |
int fd; |
265 |
FILE *fp; |
266 |
int n; |
267 |
int4 nextloc; |
268 |
/* open holodeck file */ |
269 |
if ((fp = fopen(hdkfile, "r")) == NULL) { |
270 |
sprintf(errmsg, "cannot open \"%s\" for reading", hdkfile); |
271 |
error(SYSTEM, errmsg); |
272 |
} |
273 |
/* check header format */ |
274 |
checkheader(fp, HOLOFMT, NULL); |
275 |
/* check magic number */ |
276 |
if (getw(fp) != HOLOMAGIC) { |
277 |
sprintf(errmsg, "bad magic number in holodeck file \"%s\"", |
278 |
hdkfile); |
279 |
error(USER, errmsg); |
280 |
} |
281 |
nextloc = ftell(fp); /* get stdio position */ |
282 |
fd = dup(fileno(fp)); /* dup file descriptor */ |
283 |
fclose(fp); /* done with stdio */ |
284 |
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
285 |
lseek(fd, (off_t)nextloc, 0); |
286 |
read(fd, (char *)&nextloc, sizeof(nextloc)); |
287 |
hdinit(fd, NULL); |
288 |
} |
289 |
/* allocate picture buffer */ |
290 |
mypixel = (COLOR *)bmalloc(xres*yres*sizeof(COLOR)); |
291 |
myweight = (float *)bmalloc(xres*yres*sizeof(float)); |
292 |
mydepth = (float *)bmalloc(xres*yres*sizeof(float)); |
293 |
if (mypixel == NULL | myweight == NULL | mydepth == NULL) |
294 |
error(SYSTEM, "out of memory in initialize"); |
295 |
} |
296 |
|
297 |
|
298 |
void |
299 |
eputs(s) /* put error message to stderr */ |
300 |
register char *s; |
301 |
{ |
302 |
static int midline = 0; |
303 |
|
304 |
if (!*s) |
305 |
return; |
306 |
if (!midline++) { /* prepend line with program name */ |
307 |
fputs(progname, stderr); |
308 |
fputs(": ", stderr); |
309 |
} |
310 |
fputs(s, stderr); |
311 |
if (s[strlen(s)-1] == '\n') { |
312 |
fflush(stderr); |
313 |
midline = 0; |
314 |
} |
315 |
} |