ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.3
Committed: Mon Mar 8 14:09:11 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +8 -6 lines
Log Message:
finished discontinuity implementation and fixed various bugs

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
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][2] == 'a')
54 pixaspect = atof(argv[++i]);
55 else if (argv[i][2] == '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-1)
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 #ifdef DEBUG
149 if (blist.nb > 0 & rval > 0) {
150 sprintf(errmsg, "%d unrendered pixels in frame %d (%.1f%%)",
151 rval, fn, 100.*rval/(hres*vres));
152 error(WARNING, errmsg);
153 }
154 #endif
155 }
156
157
158 render_frame(bl, nb) /* render frame from beam values */
159 register PACKHEAD *bl;
160 int nb;
161 {
162 extern int pixBeam();
163 register HDBEAMI *bil;
164 register int i;
165
166 if (nb <= 0) return;
167 if ((bil = (HDBEAMI *)malloc(nb*sizeof(HDBEAMI))) == NULL)
168 error(SYSTEM, "out of memory in render_frame");
169 for (i = nb; i--; ) {
170 bil[i].h = hdlist[bl[i].hd];
171 bil[i].b = bl[i].bi;
172 }
173 hdloadbeams(bil, nb, pixBeam);
174 pixFlush();
175 free((char *)bil);
176 }
177
178
179 startpicture(fn) /* initialize picture for rendering & output */
180 int fn;
181 {
182 extern char VersionID[];
183 double pa = pixaspect;
184 char fname[256];
185 /* compute picture resolution */
186 hres = xres; vres = yres;
187 normaspect(viewaspect(&myview), &pa, &hres, &vres);
188 /* prepare output */
189 if (outspec != NULL) {
190 sprintf(fname, outspec, fn);
191 if (freopen(fname, "w", stdout) == NULL) {
192 sprintf(errmsg, "cannot open output \"%s\"", fname);
193 error(SYSTEM, errmsg);
194 }
195 }
196 /* write header */
197 newheader("RADIANCE", stdout);
198 printf("SOFTWARE= %s\n", VersionID);
199 printf("%s %s\n", progname, hdkfile);
200 if (fn)
201 printf("FRAME=%d\n", fn);
202 fputs(VIEWSTR, stdout);
203 fprintview(&myview, stdout);
204 fputc('\n', stdout);
205 if (pa < 0.99 | pa > 1.01)
206 fputaspect(pa, stdout);
207 if (expval < 0.99 | expval > 1.01)
208 fputexpos(expval, stdout);
209 fputformat(COLRFMT, stdout);
210 fputc('\n', stdout);
211 /* write resolution (standard order) */
212 fprtresolu(hres, vres, stdout);
213 /* prepare image buffers */
214 bzero((char *)mypixel, hres*vres*sizeof(COLOR));
215 bzero((char *)myweight, hres*vres*sizeof(float));
216 bzero((char *)mydepth, hres*vres*sizeof(float));
217 }
218
219
220 int
221 endpicture() /* finish and write out pixels */
222 {
223 int nunrend = 0;
224 int v;
225 register double d;
226 register int p;
227 /* compute final pixel values */
228 for (p = hres*vres; p--; ) {
229 if (myweight[p] <= FTINY) {
230 nunrend++;
231 continue;
232 }
233 d = expval/myweight[p];
234 scalecolor(mypixel[p], d);
235 }
236 /* write each scanline */
237 for (v = vres; v--; )
238 if (fwritescan(mypixel+v*hres, hres, stdout) < 0)
239 return(-1);
240 if (fflush(stdout) == EOF)
241 return(-1);
242 return(nunrend);
243 }
244
245
246 initialize() /* initialize holodeck and buffers */
247 {
248 extern long ftell();
249 int fd;
250 FILE *fp;
251 int n;
252 int4 nextloc;
253 /* open holodeck file */
254 if ((fp = fopen(hdkfile, "r")) == NULL) {
255 sprintf(errmsg, "cannot open \"%s\" for reading", hdkfile);
256 error(SYSTEM, errmsg);
257 }
258 /* check header format */
259 checkheader(fp, HOLOFMT, NULL);
260 /* check magic number */
261 if (getw(fp) != HOLOMAGIC) {
262 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
263 hdkfile);
264 error(USER, errmsg);
265 }
266 nextloc = ftell(fp); /* get stdio position */
267 fd = dup(fileno(fp)); /* dup file descriptor */
268 fclose(fp); /* done with stdio */
269 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
270 lseek(fd, (long)nextloc, 0);
271 read(fd, (char *)&nextloc, sizeof(nextloc));
272 hdinit(fd, NULL);
273 }
274 /* allocate picture buffer */
275 mypixel = (COLOR *)bmalloc(xres*yres*sizeof(COLOR));
276 myweight = (float *)bmalloc(xres*yres*sizeof(float));
277 mydepth = (float *)bmalloc(xres*yres*sizeof(float));
278 if (mypixel == NULL | myweight == NULL | mydepth == NULL)
279 error(SYSTEM, "out of memory in initialize");
280 }
281
282
283 eputs(s) /* put error message to stderr */
284 register char *s;
285 {
286 static int midline = 0;
287
288 if (!*s)
289 return;
290 if (!midline++) { /* prepend line with program name */
291 fputs(progname, stderr);
292 fputs(": ", stderr);
293 }
294 fputs(s, stderr);
295 if (s[strlen(s)-1] == '\n') {
296 fflush(stderr);
297 midline = 0;
298 }
299 }