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