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