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 (13 days, 19 hours 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

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.39 static const char RCSid[] = "$Id: rpmain.c,v 2.38 2025/04/22 17:12:25 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * rpmain.c - main for rpict batch rendering program
6     */
7    
8 greg 2.2 #include "copyright.h"
9 greg 2.1
10 greg 2.10 #include <time.h>
11 schorsch 2.3 #include <signal.h>
12    
13     #include "platform.h"
14 schorsch 2.6 #include "rtprocess.h" /* getpid() */
15 greg 2.1 #include "ray.h"
16 greg 2.38 #include "func.h"
17 greg 2.1 #include "source.h"
18     #include "ambient.h"
19     #include "random.h"
20     #include "paths.h"
21     #include "view.h"
22 greg 2.16 #include "pmapray.h"
23 greg 2.1
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 greg 2.37
36 greg 2.1 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 greg 2.9 extern double dblur; /* depth-of-field blur parameter */
53    
54 greg 2.28 RGBPRIMP out_prims = stdprims; /* output color primitives */
55     static RGBPRIMS our_prims; /* private output color primitives */
56    
57 schorsch 2.8 static void onsig(int signo);
58     static void sigdie(int signo, char *msg);
59     static void printdefaults(void);
60 greg 2.21 /* rpict additional features */
61     #ifdef PERSIST
62     #define RPICT_FEATURES "Persist\nParallelPersist\n" \
63 greg 2.29 "ParticipatingMedia=Mist\n" \
64 greg 2.22 "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 greg 2.32 "AdaptiveShadowTesting\nOutputs=v,l\n" \
69 greg 2.33 "OutputCS=RGB,XYZ,prims\n"
70 greg 2.21 #else
71     #define RPICT_FEATURES "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \
72 greg 2.29 "ParticipatingMedia=Mist\n" \
73 greg 2.22 "HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \
74     "PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \
75     "SmallSourceDrawing\nViewSequence\nProgressReporting\n" \
76 greg 2.32 "AdaptiveShadowTesting\nOutputs=v,l\n" \
77 greg 2.33 "OutputCS=RGB,XYZ,prims\n"
78 greg 2.21 #endif
79 greg 2.1
80    
81     int
82 schorsch 2.8 main(int argc, char *argv[])
83 greg 2.1 {
84     #define check(ol,al) if (argv[i][ol] || \
85     badarg(argc-i-1,argv+i+1,al)) \
86     goto badopt
87 schorsch 2.18 #define check_bool(olen,var) switch (argv[i][olen]) { \
88 greg 2.1 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 greg 2.15 int duped1 = -1;
102 greg 2.1 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 greg 2.21 /* feature check only? */
109     strcat(RFeatureList, RPICT_FEATURES);
110 greg 2.25 if (argc > 1 && !strcmp(argv[1], "-features"))
111 greg 2.21 return feature_status(argc-2, argv+2);
112 greg 2.38 /* initialize calcomp routines */
113     initfunc();
114 greg 2.1 /* 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 greg 2.9 case 'd': /* aperture */
186     check(3,"f");
187     dblur = atof(argv[++i]);
188     break;
189 greg 2.28 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 greg 2.1 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 greg 2.36 case 't': /* timer */
259     check(2,"i");
260     ralrm = atoi(argv[++i]);
261     break;
262 greg 2.1 case 'w': /* warnings */
263     rval = erract[WARNING].pf != NULL;
264 schorsch 2.18 check_bool(2,rval);
265 greg 2.1 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 greg 2.30 /* set/check spectral sampling */
277 greg 2.31 if (setspectrsamp(CNDX, WLPART) <= 0)
278 greg 2.30 error(USER, "unsupported spectral sampling");
279    
280 greg 2.1 err = setview(&ourview); /* set viewing parameters */
281     if (err != NULL)
282     error(USER, err);
283     /* initialize object types */
284     initotypes();
285     /* initialize urand */
286 greg 2.12 if (rand_samp) {
287     srandom((long)time(0));
288     initurand(0);
289     } else {
290     srandom(0L);
291     initurand(2048);
292     }
293 greg 2.1 /* set up signal handling */
294     sigdie(SIGINT, "Interrupt");
295 schorsch 2.4 #ifdef SIGHUP
296 greg 2.1 sigdie(SIGHUP, "Hangup");
297 schorsch 2.4 #endif
298 greg 2.1 sigdie(SIGTERM, "Terminate");
299 schorsch 2.4 #ifdef SIGPIPE
300 greg 2.1 sigdie(SIGPIPE, "Broken pipe");
301 schorsch 2.4 #endif
302     #ifdef SIGALRM
303 greg 2.1 sigdie(SIGALRM, "Alarm clock");
304 schorsch 2.4 #endif
305 greg 2.1 #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 schorsch 2.3 SET_FILE_BINARY(stdout);
346 greg 2.1 if (octname == NULL)
347 schorsch 2.3 SET_FILE_BINARY(stdin);
348 greg 2.1 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 greg 2.16
356     ray_init_pmap(); /* PMAP: set up & load photon maps */
357 greg 2.1
358     marksources(); /* find and mark sources */
359     setambient(); /* initialize ambient calculation */
360 greg 2.16
361 greg 2.20 fflush(stdout); /* in case we're duplicating header */
362    
363 greg 2.1 #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 greg 2.34 cow_memshare(); /* preloads scene */
371 greg 2.1 while ((rval=fork()) == 0) { /* keep on forkin' */
372     pflock(1);
373     pfhold();
374     tstart = time((time_t *)NULL);
375 greg 2.13 ambsync(); /* load new values */
376 greg 2.1 }
377     if (rval < 0)
378     error(SYSTEM, "cannot fork child for persist function");
379 greg 2.13 pfdetach(); /* parent will run then exit */
380 greg 2.1 }
381     }
382     runagain:
383 schorsch 2.7 if (persist) {
384 greg 2.1 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 schorsch 2.7 }
389 greg 2.1 #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 greg 2.16
415    
416     ray_done_pmap(); /* PMAP: free photon maps */
417    
418 greg 2.1 quit(0);
419    
420     badopt:
421     sprintf(errmsg, "command line error at '%s'", argv[i]);
422     error(USER, errmsg);
423 schorsch 2.8 return 1; /* pro forma return */
424 greg 2.1
425     #undef check
426 schorsch 2.18 #undef check_bool
427 greg 2.1 }
428    
429    
430     void
431 schorsch 2.8 wputs( /* warning output function */
432 greg 2.26 const char *s
433 schorsch 2.8 )
434 greg 2.1 {
435     int lasterrno = errno;
436 greg 2.35 if (erract[WARNING].pf == NULL)
437     return; /* called by calcomp or someone */
438 greg 2.1 eputs(s);
439     errno = lasterrno;
440     }
441    
442    
443     void
444 schorsch 2.8 eputs( /* put string to stderr */
445 greg 2.26 const char *s
446 schorsch 2.8 )
447 greg 2.1 {
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 schorsch 2.8 static void
465     onsig( /* fatal signal */
466     int signo
467     )
468 greg 2.1 {
469     static int gotsig = 0;
470    
471     if (gotsig++) /* two signals and we're gone! */
472     _exit(signo);
473    
474 schorsch 2.4 #ifdef SIGALRM /* XXX how critical is this? */
475 greg 2.1 alarm(15); /* allow 15 seconds to clean up */
476     signal(SIGALRM, SIG_DFL); /* make certain we do die */
477 schorsch 2.4 #endif
478 greg 2.1 eputs("signal - ");
479     eputs(sigerr[signo]);
480     eputs("\n");
481     quit(3);
482     }
483    
484    
485 schorsch 2.8 static void
486     sigdie( /* set fatal signal */
487     int signo,
488     char *msg
489     )
490 greg 2.1 {
491     if (signal(signo, onsig) == SIG_IGN)
492     signal(signo, SIG_IGN);
493     sigerr[signo] = msg;
494     }
495    
496    
497 schorsch 2.8 static void
498     printdefaults(void) /* print default values to stdout */
499 greg 2.1 {
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 greg 2.14 ourview.type==VT_PLS ? "planisphere" :
507 greg 2.1 "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 greg 2.28 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 greg 2.1 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 greg 2.9 printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur);
536 greg 2.1 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     }