ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.1
Committed: Thu Mar 4 10:30:04 1999 UTC (25 years, 2 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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