ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict.c
Revision: 3.17
Committed: Thu Jan 1 11:21:55 2004 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.16: +34 -15 lines
Log Message:
Ansification and prototypes.

File Contents

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