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