ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpmain.c
Revision: 2.39
Committed: Wed Apr 23 01:57:04 2025 UTC (9 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.38: +1 -2 lines
Log Message:
fix: Functio prototypes and other fixes to reduce compiler warnings

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rpmain.c,v 2.38 2025/04/22 17:12:25 greg Exp $";
3 #endif
4 /*
5 * rpmain.c - main for rpict batch rendering program
6 */
7
8 #include "copyright.h"
9
10 #include <time.h>
11 #include <signal.h>
12
13 #include "platform.h"
14 #include "rtprocess.h" /* getpid() */
15 #include "ray.h"
16 #include "func.h"
17 #include "source.h"
18 #include "ambient.h"
19 #include "random.h"
20 #include "paths.h"
21 #include "view.h"
22 #include "pmapray.h"
23
24 /* persistent processes define */
25 #ifdef F_SETLKW
26 #define PERSIST 1 /* normal persist */
27 #define PARALLEL 2 /* parallel persist */
28 #define PCHILD 3 /* child of normal persist */
29 #endif
30
31 char *progname; /* argv[0] */
32 char *octname; /* octree name */
33 char *sigerr[NSIG]; /* signal error messages */
34 char *errfile = NULL; /* error output file */
35
36 extern time_t tstart; /* start time */
37
38 extern int ralrm; /* seconds between reports */
39
40 extern VIEW ourview; /* viewing parameters */
41
42 extern int hresolu; /* horizontal resolution */
43 extern int vresolu; /* vertical resolution */
44 extern double pixaspect; /* pixel aspect ratio */
45
46 extern int psample; /* pixel sample size */
47 extern double maxdiff; /* max. sample difference */
48 extern double dstrpix; /* square pixel distribution */
49
50 extern double mblur; /* motion blur parameter */
51
52 extern double dblur; /* depth-of-field blur parameter */
53
54 RGBPRIMP out_prims = stdprims; /* output color primitives */
55 static RGBPRIMS our_prims; /* private output color primitives */
56
57 static void onsig(int signo);
58 static void sigdie(int signo, char *msg);
59 static void printdefaults(void);
60 /* rpict additional features */
61 #ifdef PERSIST
62 #define RPICT_FEATURES "Persist\nParallelPersist\n" \
63 "ParticipatingMedia=Mist\n" \
64 "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \
65 "HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \
66 "PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \
67 "SmallSourceDrawing\nViewSequence\nProgressReporting\n" \
68 "AdaptiveShadowTesting\nOutputs=v,l\n" \
69 "OutputCS=RGB,XYZ,prims\n"
70 #else
71 #define RPICT_FEATURES "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \
72 "ParticipatingMedia=Mist\n" \
73 "HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \
74 "PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \
75 "SmallSourceDrawing\nViewSequence\nProgressReporting\n" \
76 "AdaptiveShadowTesting\nOutputs=v,l\n" \
77 "OutputCS=RGB,XYZ,prims\n"
78 #endif
79
80
81 int
82 main(int argc, char *argv[])
83 {
84 #define check(ol,al) if (argv[i][ol] || \
85 badarg(argc-i-1,argv+i+1,al)) \
86 goto badopt
87 #define check_bool(olen,var) switch (argv[i][olen]) { \
88 case '\0': var = !var; break; \
89 case 'y': case 'Y': case 't': case 'T': \
90 case '+': case '1': var = 1; break; \
91 case 'n': case 'N': case 'f': case 'F': \
92 case '-': case '0': var = 0; break; \
93 default: goto badopt; }
94 char *err;
95 char *recover = NULL;
96 char *outfile = NULL;
97 char *zfile = NULL;
98 int loadflags = ~IO_FILES;
99 int seqstart = 0;
100 int persist = 0;
101 int duped1 = -1;
102 int rval;
103 int i;
104 /* record start time */
105 tstart = time((time_t *)NULL);
106 /* global program name */
107 progname = argv[0] = fixargv0(argv[0]);
108 /* feature check only? */
109 strcat(RFeatureList, RPICT_FEATURES);
110 if (argc > 1 && !strcmp(argv[1], "-features"))
111 return feature_status(argc-2, argv+2);
112 /* initialize calcomp routines */
113 initfunc();
114 /* option city */
115 for (i = 1; i < argc; i++) {
116 /* expand arguments */
117 while ((rval = expandarg(&argc, &argv, i)) > 0)
118 ;
119 if (rval < 0) {
120 sprintf(errmsg, "cannot expand '%s'", argv[i]);
121 error(SYSTEM, errmsg);
122 }
123 if (argv[i] == NULL || argv[i][0] != '-')
124 break; /* break from options */
125 if (!strcmp(argv[i], "-version")) {
126 puts(VersionID);
127 quit(0);
128 }
129 if (!strcmp(argv[i], "-defaults") ||
130 !strcmp(argv[i], "-help")) {
131 printdefaults();
132 quit(0);
133 }
134 rval = getrenderopt(argc-i, argv+i);
135 if (rval >= 0) {
136 i += rval;
137 continue;
138 }
139 rval = getviewopt(&ourview, argc-i, argv+i);
140 if (rval >= 0) {
141 i += rval;
142 continue;
143 }
144 /* rpict options */
145 switch (argv[i][1]) {
146 case 'v': /* view file */
147 if (argv[i][2] != 'f')
148 goto badopt;
149 check(3,"s");
150 rval = viewfile(argv[++i], &ourview, NULL);
151 if (rval < 0) {
152 sprintf(errmsg,
153 "cannot open view file \"%s\"",
154 argv[i]);
155 error(SYSTEM, errmsg);
156 } else if (rval == 0) {
157 sprintf(errmsg,
158 "bad view file \"%s\"",
159 argv[i]);
160 error(USER, errmsg);
161 }
162 break;
163 case 'p': /* pixel */
164 switch (argv[i][2]) {
165 case 's': /* sample */
166 check(3,"i");
167 psample = atoi(argv[++i]);
168 break;
169 case 't': /* threshold */
170 check(3,"f");
171 maxdiff = atof(argv[++i]);
172 break;
173 case 'j': /* jitter */
174 check(3,"f");
175 dstrpix = atof(argv[++i]);
176 break;
177 case 'a': /* aspect */
178 check(3,"f");
179 pixaspect = atof(argv[++i]);
180 break;
181 case 'm': /* motion */
182 check(3,"f");
183 mblur = atof(argv[++i]);
184 break;
185 case 'd': /* aperture */
186 check(3,"f");
187 dblur = atof(argv[++i]);
188 break;
189 case 'R': /* standard RGB output */
190 if (strcmp(argv[i]+2, "RGB"))
191 goto badopt;
192 out_prims = stdprims;
193 break;
194 case 'X': /* XYZ output */
195 if (strcmp(argv[i]+2, "XYZ"))
196 goto badopt;
197 out_prims = xyzprims;
198 break;
199 case 'c': { /* chromaticities */
200 int j;
201 check(3,"ffffffff");
202 rval = 0;
203 for (j = 0; j < 8; j++) {
204 our_prims[0][j] = atof(argv[++i]);
205 rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001;
206 }
207 if (rval) {
208 if (!colorprimsOK(our_prims))
209 error(USER, "illegal primary chromaticities");
210 out_prims = our_prims;
211 } else
212 out_prims = stdprims;
213 } break;
214 default:
215 goto badopt;
216 }
217 break;
218 case 'x': /* x resolution */
219 check(2,"i");
220 hresolu = atoi(argv[++i]);
221 break;
222 case 'y': /* y resolution */
223 check(2,"i");
224 vresolu = atoi(argv[++i]);
225 break;
226 case 'S': /* slave index */
227 check(2,"i");
228 seqstart = atoi(argv[++i]);
229 break;
230 case 'o': /* output file */
231 check(2,"s");
232 outfile = argv[++i];
233 break;
234 case 'z': /* z file */
235 check(2,"s");
236 zfile = argv[++i];
237 break;
238 case 'r': /* recover file */
239 if (argv[i][2] == 'o') { /* +output */
240 check(3,"s");
241 outfile = argv[i+1];
242 } else
243 check(2,"s");
244 recover = argv[++i];
245 break;
246 #ifdef PERSIST
247 case 'P': /* persist file */
248 if (argv[i][2] == 'P') {
249 check(3,"s");
250 persist = PARALLEL;
251 } else {
252 check(2,"s");
253 persist = PERSIST;
254 }
255 persistfile(argv[++i]);
256 break;
257 #endif
258 case 't': /* timer */
259 check(2,"i");
260 ralrm = atoi(argv[++i]);
261 break;
262 case 'w': /* warnings */
263 rval = erract[WARNING].pf != NULL;
264 check_bool(2,rval);
265 if (rval) erract[WARNING].pf = wputs;
266 else erract[WARNING].pf = NULL;
267 break;
268 case 'e': /* error file */
269 check(2,"s");
270 errfile = argv[++i];
271 break;
272 default:
273 goto badopt;
274 }
275 }
276 /* set/check spectral sampling */
277 if (setspectrsamp(CNDX, WLPART) <= 0)
278 error(USER, "unsupported spectral sampling");
279
280 err = setview(&ourview); /* set viewing parameters */
281 if (err != NULL)
282 error(USER, err);
283 /* initialize object types */
284 initotypes();
285 /* initialize urand */
286 if (rand_samp) {
287 srandom((long)time(0));
288 initurand(0);
289 } else {
290 srandom(0L);
291 initurand(2048);
292 }
293 /* set up signal handling */
294 sigdie(SIGINT, "Interrupt");
295 #ifdef SIGHUP
296 sigdie(SIGHUP, "Hangup");
297 #endif
298 sigdie(SIGTERM, "Terminate");
299 #ifdef SIGPIPE
300 sigdie(SIGPIPE, "Broken pipe");
301 #endif
302 #ifdef SIGALRM
303 sigdie(SIGALRM, "Alarm clock");
304 #endif
305 #ifdef SIGXCPU
306 sigdie(SIGXCPU, "CPU limit exceeded");
307 sigdie(SIGXFSZ, "File size exceeded");
308 #endif
309 /* open error file */
310 if (errfile != NULL) {
311 if (freopen(errfile, "a", stderr) == NULL)
312 quit(2);
313 fprintf(stderr, "**************\n*** PID %5d: ",
314 getpid());
315 printargs(argc, argv, stderr);
316 putc('\n', stderr);
317 fflush(stderr);
318 }
319 #ifdef NICE
320 nice(NICE); /* lower priority */
321 #endif
322 /* get octree */
323 if (i == argc)
324 octname = NULL;
325 else if (i == argc-1)
326 octname = argv[i];
327 else
328 goto badopt;
329 if (seqstart > 0 && octname == NULL)
330 error(USER, "missing octree argument");
331 /* set up output */
332 #ifdef PERSIST
333 if (persist) {
334 if (recover != NULL)
335 error(USER, "persist option used with recover file");
336 if (seqstart <= 0)
337 error(USER, "persist option only for sequences");
338 if (outfile == NULL)
339 duped1 = dup(fileno(stdout)); /* don't lose our output */
340 openheader();
341 } else
342 #endif
343 if (outfile != NULL)
344 openheader();
345 SET_FILE_BINARY(stdout);
346 if (octname == NULL)
347 SET_FILE_BINARY(stdin);
348 readoct(octname, loadflags, &thescene, NULL);
349 nsceneobjs = nobjects;
350
351 if (loadflags & IO_INFO) { /* print header */
352 printargs(i, argv, stdout);
353 printf("SOFTWARE= %s\n", VersionID);
354 }
355
356 ray_init_pmap(); /* PMAP: set up & load photon maps */
357
358 marksources(); /* find and mark sources */
359 setambient(); /* initialize ambient calculation */
360
361 fflush(stdout); /* in case we're duplicating header */
362
363 #ifdef PERSIST
364 if (persist) {
365 if (outfile == NULL) { /* reconnect stdout */
366 dup2(duped1, fileno(stdout));
367 close(duped1);
368 }
369 if (persist == PARALLEL) { /* multiprocessing */
370 cow_memshare(); /* preloads scene */
371 while ((rval=fork()) == 0) { /* keep on forkin' */
372 pflock(1);
373 pfhold();
374 tstart = time((time_t *)NULL);
375 ambsync(); /* load new values */
376 }
377 if (rval < 0)
378 error(SYSTEM, "cannot fork child for persist function");
379 pfdetach(); /* parent will run then exit */
380 }
381 }
382 runagain:
383 if (persist) {
384 if (outfile == NULL) /* if out to stdout */
385 dupheader(); /* send header */
386 else /* if out to file */
387 duped1 = dup(fileno(stdout)); /* hang onto pipe */
388 }
389 #endif
390 /* batch render picture(s) */
391 rpict(seqstart, outfile, zfile, recover);
392 /* flush ambient file */
393 ambsync();
394 #ifdef PERSIST
395 if (persist == PERSIST) { /* first run-through */
396 if ((rval=fork()) == 0) { /* child loops until killed */
397 pflock(1);
398 persist = PCHILD;
399 } else { /* original process exits */
400 if (rval < 0)
401 error(SYSTEM, "cannot fork child for persist function");
402 pfdetach(); /* parent exits */
403 }
404 }
405 if (persist == PCHILD) { /* wait for a signal then go again */
406 if (outfile != NULL)
407 close(duped1); /* release output handle */
408 pfhold();
409 tstart = time((time_t *)NULL); /* reinitialize */
410 raynum = nrays = 0;
411 goto runagain;
412 }
413 #endif
414
415
416 ray_done_pmap(); /* PMAP: free photon maps */
417
418 quit(0);
419
420 badopt:
421 sprintf(errmsg, "command line error at '%s'", argv[i]);
422 error(USER, errmsg);
423 return 1; /* pro forma return */
424
425 #undef check
426 #undef check_bool
427 }
428
429
430 void
431 wputs( /* warning output function */
432 const char *s
433 )
434 {
435 int lasterrno = errno;
436 if (erract[WARNING].pf == NULL)
437 return; /* called by calcomp or someone */
438 eputs(s);
439 errno = lasterrno;
440 }
441
442
443 void
444 eputs( /* put string to stderr */
445 const char *s
446 )
447 {
448 static int midline = 0;
449
450 if (!*s)
451 return;
452 if (!midline++) {
453 fputs(progname, stderr);
454 fputs(": ", stderr);
455 }
456 fputs(s, stderr);
457 if (s[strlen(s)-1] == '\n') {
458 fflush(stderr);
459 midline = 0;
460 }
461 }
462
463
464 static void
465 onsig( /* fatal signal */
466 int signo
467 )
468 {
469 static int gotsig = 0;
470
471 if (gotsig++) /* two signals and we're gone! */
472 _exit(signo);
473
474 #ifdef SIGALRM /* XXX how critical is this? */
475 alarm(15); /* allow 15 seconds to clean up */
476 signal(SIGALRM, SIG_DFL); /* make certain we do die */
477 #endif
478 eputs("signal - ");
479 eputs(sigerr[signo]);
480 eputs("\n");
481 quit(3);
482 }
483
484
485 static void
486 sigdie( /* set fatal signal */
487 int signo,
488 char *msg
489 )
490 {
491 if (signal(signo, onsig) == SIG_IGN)
492 signal(signo, SIG_IGN);
493 sigerr[signo] = msg;
494 }
495
496
497 static void
498 printdefaults(void) /* print default values to stdout */
499 {
500 printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
501 ourview.type==VT_PER ? "perspective" :
502 ourview.type==VT_PAR ? "parallel" :
503 ourview.type==VT_HEM ? "hemispherical" :
504 ourview.type==VT_ANG ? "angular" :
505 ourview.type==VT_CYL ? "cylindrical" :
506 ourview.type==VT_PLS ? "planisphere" :
507 "unknown");
508 printf("-vp %f %f %f\t# view point\n",
509 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
510 printf("-vd %f %f %f\t# view direction\n",
511 ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
512 printf("-vu %f %f %f\t# view up\n",
513 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
514 printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
515 printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
516 printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore);
517 printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft);
518 printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
519 printf("-vl %f\t\t\t# view lift\n", ourview.voff);
520 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
521 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
522 if (out_prims == stdprims)
523 printf("-pRGB\t\t\t\t# standard RGB color output\n");
524 else if (out_prims == xyzprims)
525 printf("-pXYZ\t\t\t\t# CIE XYZ color output\n");
526 else if (out_prims != NULL)
527 printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n",
528 out_prims[RED][0], out_prims[RED][1],
529 out_prims[GRN][0], out_prims[GRN][1],
530 out_prims[BLU][0], out_prims[BLU][1],
531 out_prims[WHT][0], out_prims[WHT][1]);
532 printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect);
533 printf("-pj %f\t\t\t# pixel jitter\n", dstrpix);
534 printf("-pm %f\t\t\t# pixel motion\n", mblur);
535 printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur);
536 printf("-ps %-9d\t\t\t# pixel sample\n", psample);
537 printf("-pt %f\t\t\t# pixel threshold\n", maxdiff);
538 printf("-t %-9d\t\t\t# time between reports\n", ralrm);
539 printf(erract[WARNING].pf != NULL ?
540 "-w+\t\t\t\t# warning messages on\n" :
541 "-w-\t\t\t\t# warning messages off\n");
542 print_rdefaults();
543 }