ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.7
Committed: Tue Mar 9 15:10:26 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.6: +4 -1 lines
Log Message:
added printing of command arguments in header

File Contents

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