ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.18
Committed: Tue Jun 8 19:48:30 2004 UTC (19 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Changes since 3.17: +1 -2 lines
Log Message:
Removed redundant #include's and fixed ordering on some headers

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rhpict.c,v 3.17 2004/01/01 11:21:55 schorsch Exp $";
3 #endif
4 /*
5 * Radiance holodeck picture generator
6 */
7
8 #include <string.h>
9
10 #include "platform.h"
11 #include "rterror.h"
12 #include "rholo.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 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
43
44 int
45 main(
46 int argc,
47 char *argv[]
48 )
49 {
50 int i, rval;
51
52 gargc = argc; gargv = argv;
53 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 if (argv[i][2] == 'a')
68 pixaspect = atof(argv[++i]);
69 else if (argv[i][2] == 'e') {
70 expval = atof(argv[++i]);
71 if ((argv[i][0] == '-') | (argv[i][0] == '+'))
72 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 case 'r': /* random sampling */
92 if (badarg(argc-i-1,argv+i+1,"f"))
93 goto userr;
94 randfrac = atof(argv[++i]);
95 break;
96 case 's': /* smooth sampling */
97 randfrac = -1.;
98 break;
99 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 if (argv[i][2]!='f' || badarg(argc-i-1,argv+i+1,"s"))
106 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 if (i != argc-1)
124 goto userr;
125 hdkfile = argv[i];
126 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 "Usage: %s [-w][-r rf][-pa pa][-pe ex][-x hr][-y vr][-S stfn][-o outp][view] input.hdk\n",
137 progname);
138 quit(1);
139 return 1; /* pro forma return */
140 }
141
142
143 static void
144 dopicture( /* render view from holodeck */
145 int fn
146 )
147 {
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 free((void *)blist.bl);
164 } 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 #ifdef DEBUG
174 if (blist.nb > 0 & rval > 0) {
175 sprintf(errmsg, "%d unrendered pixels in frame %d (%.1f%%)",
176 rval, fn, 100.*rval/(hres*vres));
177 error(WARNING, errmsg);
178 }
179 #endif
180 }
181
182
183 static void
184 render_frame( /* render frame from beam values */
185 register PACKHEAD *bl,
186 int nb
187 )
188 {
189 register HDBEAMI *bil;
190 register int i;
191
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 hdloadbeams(bil, nb, pixBeam);
200 pixFinish(randfrac);
201 free((void *)bil);
202 }
203
204
205 static void
206 startpicture( /* initialize picture for rendering & output */
207 int fn
208 )
209 {
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 if (freopen(fname, "w", stdout) == NULL) {
220 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 printargs(gargc, gargv, stdout);
228 if (fn)
229 printf("FRAME=%d\n", fn);
230 fputs(VIEWSTR, stdout);
231 fprintview(&myview, stdout);
232 fputc('\n', stdout);
233 if ((pa < 0.99) | (pa > 1.01))
234 fputaspect(pa, stdout);
235 if ((expval < 0.99) | (expval > 1.01))
236 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 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 }
246
247
248 static int
249 endpicture(void) /* finish and write out pixels */
250 {
251 int lastr = -1, nunrend = 0;
252 int32 lastp, lastrp;
253 register int32 p;
254 register double d;
255 /* compute final pixel values */
256 for (p = hres*vres; p--; ) {
257 if (myweight[p] <= FTINY) {
258 if (lastr >= 0) {
259 if (p/hres == lastp/hres)
260 copycolor(mypixel[p], mypixel[lastp]);
261 else
262 copycolor(mypixel[p], mypixel[lastrp]);
263 }
264 nunrend++;
265 continue;
266 }
267 d = expval/myweight[p];
268 scalecolor(mypixel[p], d);
269 if ((lastp=p)/hres != lastr)
270 lastr = (lastrp=p)/hres;
271 }
272 /* write each scanline */
273 for (p = vres; p--; )
274 if (fwritescan(mypixel+p*hres, hres, stdout) < 0)
275 return(-1);
276 if (fflush(stdout) == EOF)
277 return(-1);
278 return(nunrend);
279 }
280
281
282 static void
283 initialize(void) /* initialize holodeck and buffers */
284 {
285 int fd;
286 FILE *fp;
287 int n;
288 int32 nextloc;
289 /* open holodeck file */
290 if ((fp = fopen(hdkfile, "r")) == NULL) {
291 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 lseek(fd, (off_t)nextloc, SEEK_SET);
307 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 mydepth = (float *)bmalloc(xres*yres*sizeof(float));
314 if ((mypixel == NULL) | (myweight == NULL) | (mydepth == NULL))
315 error(SYSTEM, "out of memory in initialize");
316 }
317
318
319 void
320 eputs(s) /* put error message to stderr */
321 register char *s;
322 {
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 }