ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.4
Committed: Tue Mar 9 10:55:24 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +13 -6 lines
Log Message:
added brain-dead pixel filling for unrendered pixels

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][2]!='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 lastr = -1, nunrend = 0;
224 int4 lastp, lastrp;
225 register int4 p;
226 register double d;
227 /* compute final pixel values */
228 for (p = hres*vres; p--; ) {
229 if (myweight[p] <= FTINY) {
230 if (lastr >= 0)
231 if (p/hres == lastp/hres)
232 copycolor(mypixel[p], mypixel[lastp]);
233 else
234 copycolor(mypixel[p], mypixel[lastrp]);
235 nunrend++;
236 continue;
237 }
238 d = expval/myweight[p];
239 scalecolor(mypixel[p], d);
240 if ((lastp=p)/hres != lastr)
241 lastr = (lastrp=p)/hres;
242 }
243 /* write each scanline */
244 for (p = vres; p--; )
245 if (fwritescan(mypixel+p*hres, hres, stdout) < 0)
246 return(-1);
247 if (fflush(stdout) == EOF)
248 return(-1);
249 return(nunrend);
250 }
251
252
253 initialize() /* initialize holodeck and buffers */
254 {
255 extern long ftell();
256 int fd;
257 FILE *fp;
258 int n;
259 int4 nextloc;
260 /* open holodeck file */
261 if ((fp = fopen(hdkfile, "r")) == NULL) {
262 sprintf(errmsg, "cannot open \"%s\" for reading", hdkfile);
263 error(SYSTEM, errmsg);
264 }
265 /* check header format */
266 checkheader(fp, HOLOFMT, NULL);
267 /* check magic number */
268 if (getw(fp) != HOLOMAGIC) {
269 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
270 hdkfile);
271 error(USER, errmsg);
272 }
273 nextloc = ftell(fp); /* get stdio position */
274 fd = dup(fileno(fp)); /* dup file descriptor */
275 fclose(fp); /* done with stdio */
276 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
277 lseek(fd, (long)nextloc, 0);
278 read(fd, (char *)&nextloc, sizeof(nextloc));
279 hdinit(fd, NULL);
280 }
281 /* allocate picture buffer */
282 mypixel = (COLOR *)bmalloc(xres*yres*sizeof(COLOR));
283 myweight = (float *)bmalloc(xres*yres*sizeof(float));
284 mydepth = (float *)bmalloc(xres*yres*sizeof(float));
285 if (mypixel == NULL | myweight == NULL | mydepth == NULL)
286 error(SYSTEM, "out of memory in initialize");
287 }
288
289
290 eputs(s) /* put error message to stderr */
291 register char *s;
292 {
293 static int midline = 0;
294
295 if (!*s)
296 return;
297 if (!midline++) { /* prepend line with program name */
298 fputs(progname, stderr);
299 fputs(": ", stderr);
300 }
301 fputs(s, stderr);
302 if (s[strlen(s)-1] == '\n') {
303 fflush(stderr);
304 midline = 0;
305 }
306 }