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