ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.16
Committed: Wed Oct 22 02:06:34 2003 UTC (20 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.15: +2 -2 lines
Log Message:
Fewer complaints if "platform.h" precedes "standard.h"

File Contents

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