ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.34
Committed: Wed Nov 24 10:05:53 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +1 -1 lines
Log Message:
fixed use of uname()

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rpict.c - routines and variables for picture generation.
9 *
10 * 8/14/85
11 */
12
13 #include "ray.h"
14
15 #include <sys/types.h>
16
17 #ifndef NIX
18 #ifdef BSD
19 #include <sys/time.h>
20 #include <sys/resource.h>
21 #else
22 #include <sys/times.h>
23 #include <sys/utsname.h>
24 #include <unistd.h>
25 #endif
26 #endif
27
28 extern time_t time();
29
30 #include <signal.h>
31
32 #include "view.h"
33
34 #include "resolu.h"
35
36 #include "random.h"
37
38 #include "paths.h"
39
40 #define RFTEMPLATE "rfXXXXXX"
41
42 #ifndef SIGCONT
43 #define SIGCONT SIGIO
44 #endif
45
46 int dimlist[MAXDIM]; /* sampling dimensions */
47 int ndims = 0; /* number of sampling dimensions */
48 int samplendx; /* sample index number */
49
50 VIEW ourview = STDVIEW; /* view parameters */
51 int hresolu = 512; /* horizontal resolution */
52 int vresolu = 512; /* vertical resolution */
53 double pixaspect = 1.0; /* pixel aspect ratio */
54
55 int psample = 4; /* pixel sample size */
56 double maxdiff = .05; /* max. difference for interpolation */
57 double dstrpix = 0.67; /* square pixel distribution */
58
59 double dstrsrc = 0.0; /* square source distribution */
60 double shadthresh = .05; /* shadow threshold */
61 double shadcert = .5; /* shadow certainty */
62 int directrelay = 1; /* number of source relays */
63 int vspretest = 512; /* virtual source pretest density */
64 int directvis = 1; /* sources visible? */
65 double srcsizerat = .25; /* maximum ratio source size/dist. */
66
67 double specthresh = .15; /* specular sampling threshold */
68 double specjitter = 1.; /* specular sampling jitter */
69
70 int maxdepth = 6; /* maximum recursion depth */
71 double minweight = 5e-3; /* minimum ray weight */
72
73 COLOR ambval = BLKCOLOR; /* ambient value */
74 double ambacc = 0.2; /* ambient accuracy */
75 int ambres = 32; /* ambient resolution */
76 int ambdiv = 128; /* ambient divisions */
77 int ambssamp = 0; /* ambient super-samples */
78 int ambounce = 0; /* ambient bounces */
79 char *amblist[128]; /* ambient include/exclude list */
80 int ambincl = -1; /* include == 1, exclude == 0 */
81
82 #ifdef MSDOS
83 int ralrm = 60; /* seconds between reports */
84 #else
85 int ralrm = 0; /* seconds between reports */
86 #endif
87
88 double pctdone = 0.0; /* percentage done */
89
90 time_t tlastrept = 0L; /* time at last report */
91
92 extern time_t tstart; /* starting time */
93
94 extern unsigned long nrays; /* number of rays traced */
95
96 #define MAXDIV 16 /* maximum sample size */
97
98 #define pixjitter() (.5+dstrpix*(.5-frandom()))
99
100 static int hres, vres; /* resolution for this frame */
101
102 extern char *mktemp();
103
104 double pixvalue();
105
106 #ifdef NIX
107 #define file_exists(f) (access(f,F_OK)==0)
108 #else
109 #include <sys/types.h>
110 #include <sys/stat.h>
111 int
112 file_exists(fname) /* ordinary file exists? */
113 char *fname;
114 {
115 struct stat sbuf;
116 if (stat(fname, &sbuf) < 0) return(0);
117 return((sbuf.st_mode & S_IFREG) != 0);
118 }
119 #endif
120
121
122 quit(code) /* quit program */
123 int code;
124 {
125 if (code) /* report status */
126 report();
127 headclean(); /* delete header file */
128 pfclean(); /* clean up persist files */
129 exit(code);
130 }
131
132
133 #ifndef NIX
134 report() /* report progress */
135 {
136 double u, s;
137 #ifdef BSD
138 char hostname[257];
139 struct rusage rubuf;
140 #else
141 struct tms tbuf;
142 struct utsname nambuf;
143 double period;
144 #define hostname nambuf.nodename
145 #endif
146
147 tlastrept = time((time_t *)NULL);
148 #ifdef BSD
149 getrusage(RUSAGE_SELF, &rubuf);
150 u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
151 s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
152 getrusage(RUSAGE_CHILDREN, &rubuf);
153 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
154 s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
155 gethostname(hostname, sizeof(hostname));
156 #else
157 times(&tbuf);
158 period = 1.0 / sysconf(_SC_CLK_TCK);
159 u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
160 s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
161 uname(&nambuf);
162 #endif
163
164 sprintf(errmsg,
165 "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
166 nrays, pctdone, u/3600., s/3600.,
167 (tlastrept-tstart)/3600., hostname);
168 eputs(errmsg);
169 #undef hostname
170 }
171 #else
172 report() /* report progress */
173 {
174 tlastrept = time((time_t *)NULL);
175 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
176 nrays, pctdone, (tlastrept-tstart)/3600.0);
177 eputs(errmsg);
178 signal(SIGCONT, report);
179 }
180 #endif
181
182
183 rpict(seq, pout, zout, prvr) /* generate image(s) */
184 int seq;
185 char *pout, *zout, *prvr;
186 /*
187 * If seq is greater than zero, then we will render a sequence of
188 * images based on view parameter strings read from the standard input.
189 * If pout is NULL, then all images will be sent to the standard ouput.
190 * If seq is greater than zero and prvr is an integer, then it is the
191 * frame number at which rendering should begin. Preceeding view parameter
192 * strings will be skipped in the input.
193 * If pout and prvr are the same, prvr is renamed to avoid overwriting.
194 * Note that pout and zout should contain %d format specifications for
195 * sequenced file naming.
196 */
197 {
198 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
199 char fbuf[128], fbuf2[128];
200 int npicts;
201 register char *cp;
202 RESOLU rs;
203 double pa;
204 /* check sampling */
205 if (psample < 1)
206 psample = 1;
207 else if (psample > MAXDIV) {
208 sprintf(errmsg, "pixel sampling reduced from %d to %d",
209 psample, MAXDIV);
210 error(WARNING, errmsg);
211 psample = MAXDIV;
212 }
213 /* get starting frame */
214 if (seq <= 0)
215 seq = 0;
216 else if (prvr != NULL && isint(prvr)) {
217 register int rn; /* skip to specified view */
218 if ((rn = atoi(prvr)) < seq)
219 error(USER, "recover frame less than start frame");
220 if (pout == NULL)
221 error(USER, "missing output file specification");
222 for ( ; seq < rn; seq++)
223 if (nextview(stdin) == EOF)
224 error(USER, "unexpected EOF on view input");
225 prvr = fbuf; /* mark for renaming */
226 }
227 if (pout != NULL & prvr != NULL) {
228 sprintf(fbuf, pout, seq);
229 if (!strcmp(prvr, fbuf)) { /* rename */
230 strcpy(fbuf2, fbuf);
231 for (cp = fbuf2; *cp; cp++)
232 ;
233 while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
234 cp--;
235 strcpy(cp, RFTEMPLATE);
236 prvr = mktemp(fbuf2);
237 if (rename(fbuf, prvr) < 0)
238 if (errno == ENOENT) { /* ghost file */
239 sprintf(errmsg,
240 "new output file \"%s\"",
241 fbuf);
242 error(WARNING, errmsg);
243 prvr = NULL;
244 } else { /* serious error */
245 sprintf(errmsg,
246 "cannot rename \"%s\" to \"%s\"",
247 fbuf, prvr);
248 error(SYSTEM, errmsg);
249 }
250 }
251 }
252 npicts = 0; /* render sequence */
253 do {
254 if (seq && nextview(stdin) == EOF)
255 break;
256 pctdone = 0.0;
257 if (pout != NULL) {
258 sprintf(fbuf, pout, seq);
259 if (file_exists(fbuf)) {
260 if (prvr != NULL || !strcmp(fbuf, pout)) {
261 sprintf(errmsg,
262 "output file \"%s\" exists",
263 fbuf);
264 error(USER, errmsg);
265 }
266 continue; /* don't clobber */
267 }
268 if (freopen(fbuf, "w", stdout) == NULL) {
269 sprintf(errmsg,
270 "cannot open output file \"%s\"", fbuf);
271 error(SYSTEM, errmsg);
272 }
273 #ifdef MSDOS
274 setmode(fileno(stdout), O_BINARY);
275 #endif
276 dupheader();
277 }
278 hres = hresolu; vres = vresolu; pa = pixaspect;
279 if (prvr != NULL)
280 if (viewfile(prvr, &ourview, &rs) <= 0
281 || rs.or != PIXSTANDARD) {
282 sprintf(errmsg,
283 "cannot recover view parameters from \"%s\"", prvr);
284 error(WARNING, errmsg);
285 } else {
286 pa = 0.0;
287 hres = scanlen(&rs);
288 vres = numscans(&rs);
289 }
290 if ((cp = setview(&ourview)) != NULL)
291 error(USER, cp);
292 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
293 if (seq) {
294 if (ralrm > 0) {
295 fprintf(stderr, "FRAME %d:", seq);
296 fprintview(&ourview, stderr);
297 putc('\n', stderr);
298 fflush(stderr);
299 }
300 printf("FRAME=%d\n", seq);
301 }
302 fputs(VIEWSTR, stdout);
303 fprintview(&ourview, stdout);
304 putchar('\n');
305 if (pa < .99 || pa > 1.01)
306 fputaspect(pa, stdout);
307 fputformat(COLRFMT, stdout);
308 putchar('\n');
309 if (zout != NULL)
310 sprintf(cp=fbuf, zout, seq);
311 else
312 cp = NULL;
313 render(cp, prvr);
314 prvr = NULL;
315 npicts++;
316 } while (seq++);
317 /* check that we did something */
318 if (npicts == 0)
319 error(WARNING, "no output produced");
320 }
321
322
323 nextview(fp) /* get next view from fp */
324 FILE *fp;
325 {
326 char linebuf[256];
327
328 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
329 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
330 return(0);
331 return(EOF);
332 }
333
334
335 render(zfile, oldfile) /* render the scene */
336 char *zfile, *oldfile;
337 {
338 extern long lseek();
339 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
340 float *zbar[MAXDIV+1]; /* z values */
341 char *sampdens; /* previous sample density */
342 int ypos; /* current scanline */
343 int ystep; /* current y step size */
344 int hstep; /* h step size */
345 int zfd;
346 COLOR *colptr;
347 float *zptr;
348 register int i;
349 /* allocate scanlines */
350 for (i = 0; i <= psample; i++) {
351 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
352 if (scanbar[i] == NULL)
353 goto memerr;
354 }
355 hstep = (psample*140+49)/99; /* quincunx sampling */
356 ystep = (psample*99+70)/140;
357 if (hstep > 2) {
358 i = hres/hstep + 2;
359 if ((sampdens = malloc(i)) == NULL)
360 goto memerr;
361 while (i--)
362 sampdens[i] = hstep;
363 } else
364 sampdens = NULL;
365 /* open z-file */
366 if (zfile != NULL) {
367 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
368 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
369 error(SYSTEM, errmsg);
370 }
371 #ifdef MSDOS
372 setmode(zfd, O_BINARY);
373 #endif
374 for (i = 0; i <= psample; i++) {
375 zbar[i] = (float *)malloc(hres*sizeof(float));
376 if (zbar[i] == NULL)
377 goto memerr;
378 }
379 } else {
380 zfd = -1;
381 for (i = 0; i <= psample; i++)
382 zbar[i] = NULL;
383 }
384 /* write out boundaries */
385 fprtresolu(hres, vres, stdout);
386 /* recover file and compute first */
387 i = salvage(oldfile);
388 if (zfd != -1 && i > 0 &&
389 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
390 error(SYSTEM, "z-file seek error in render");
391 pctdone = 100.0*i/vres;
392 if (ralrm > 0) /* report init stats */
393 report();
394 #ifndef BSD
395 else
396 #endif
397 signal(SIGCONT, report);
398 ypos = vres-1 - i;
399 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
400 /* compute scanlines */
401 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
402 /* bottom adjust? */
403 if (ypos < 0) {
404 ystep += ypos;
405 ypos = 0;
406 }
407 colptr = scanbar[ystep]; /* move base to top */
408 scanbar[ystep] = scanbar[0];
409 scanbar[0] = colptr;
410 zptr = zbar[ystep];
411 zbar[ystep] = zbar[0];
412 zbar[0] = zptr;
413 /* fill base line */
414 fillscanline(scanbar[0], zbar[0], sampdens,
415 hres, ypos, hstep);
416 /* fill bar */
417 fillscanbar(scanbar, zbar, hres, ypos, ystep);
418 /* write it out */
419 #ifndef BSD
420 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
421 #endif
422 for (i = ystep; i > 0; i--) {
423 if (zfd != -1 && write(zfd, (char *)zbar[i],
424 hres*sizeof(float))
425 < hres*sizeof(float))
426 goto writerr;
427 if (fwritescan(scanbar[i], hres, stdout) < 0)
428 goto writerr;
429 }
430 if (fflush(stdout) == EOF)
431 goto writerr;
432 /* record progress */
433 pctdone = 100.0*(vres-1-ypos)/vres;
434 if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
435 report();
436 #ifndef BSD
437 else
438 signal(SIGCONT, report);
439 #endif
440 }
441 /* clean up */
442 signal(SIGCONT, SIG_IGN);
443 if (zfd != -1) {
444 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
445 < hres*sizeof(float))
446 goto writerr;
447 if (close(zfd) == -1)
448 goto writerr;
449 for (i = 0; i <= psample; i++)
450 free((char *)zbar[i]);
451 }
452 fwritescan(scanbar[0], hres, stdout);
453 if (fflush(stdout) == EOF)
454 goto writerr;
455 for (i = 0; i <= psample; i++)
456 free((char *)scanbar[i]);
457 if (sampdens != NULL)
458 free(sampdens);
459 pctdone = 100.0;
460 if (ralrm > 0)
461 report();
462 signal(SIGCONT, SIG_DFL);
463 return;
464 writerr:
465 error(SYSTEM, "write error in render");
466 memerr:
467 error(SYSTEM, "out of memory in render");
468 }
469
470
471 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
472 register COLOR *scanline;
473 register float *zline;
474 register char *sd;
475 int xres, y, xstep;
476 {
477 static int nc = 0; /* number of calls */
478 int bl = xstep, b = xstep;
479 double z;
480 register int i;
481
482 z = pixvalue(scanline[0], 0, y);
483 if (zline) zline[0] = z;
484 /* zig-zag start for quincunx pattern */
485 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
486 if (i >= xres) {
487 xstep += xres-1-i;
488 i = xres-1;
489 }
490 z = pixvalue(scanline[i], i, y);
491 if (zline) zline[i] = z;
492 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
493 if (i <= xstep)
494 b = fillsample(scanline, zline, 0, y, i, 0, b/2);
495 else
496 b = fillsample(scanline+i-xstep,
497 zline ? zline+i-xstep : NULL,
498 i-xstep, y, xstep, 0, b/2);
499 if (sd) *sd++ = nc & 1 ? bl : b;
500 bl = b;
501 }
502 if (sd && nc & 1) *sd = bl;
503 }
504
505
506 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
507 register COLOR *scanbar[];
508 register float *zbar[];
509 int xres, y, ysize;
510 {
511 COLOR vline[MAXDIV+1];
512 float zline[MAXDIV+1];
513 int b = ysize;
514 register int i, j;
515
516 for (i = 0; i < xres; i++) {
517
518 copycolor(vline[0], scanbar[0][i]);
519 copycolor(vline[ysize], scanbar[ysize][i]);
520 if (zbar[0]) {
521 zline[0] = zbar[0][i];
522 zline[ysize] = zbar[ysize][i];
523 }
524
525 b = fillsample(vline, zbar[0] ? zline : NULL,
526 i, y, 0, ysize, b/2);
527
528 for (j = 1; j < ysize; j++)
529 copycolor(scanbar[j][i], vline[j]);
530 if (zbar[0])
531 for (j = 1; j < ysize; j++)
532 zbar[j][i] = zline[j];
533 }
534 }
535
536
537 int
538 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
539 register COLOR *colline;
540 register float *zline;
541 int x, y;
542 int xlen, ylen;
543 int b;
544 {
545 double ratio;
546 double z;
547 COLOR ctmp;
548 int ncut;
549 register int len;
550
551 if (xlen > 0) /* x or y length is zero */
552 len = xlen;
553 else
554 len = ylen;
555
556 if (len <= 1) /* limit recursion */
557 return(0);
558
559 if (b > 0
560 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
561 || bigdiff(colline[0], colline[len], maxdiff)) {
562
563 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
564 if (zline) zline[len>>1] = z;
565 ncut = 1;
566
567 } else { /* interpolate */
568
569 copycolor(colline[len>>1], colline[len]);
570 ratio = (double)(len>>1) / len;
571 scalecolor(colline[len>>1], ratio);
572 if (zline) zline[len>>1] = zline[len] * ratio;
573 ratio = 1.0 - ratio;
574 copycolor(ctmp, colline[0]);
575 scalecolor(ctmp, ratio);
576 addcolor(colline[len>>1], ctmp);
577 if (zline) zline[len>>1] += zline[0] * ratio;
578 ncut = 0;
579 }
580 /* recurse */
581 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
582
583 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
584 x+(xlen>>1), y+(ylen>>1),
585 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
586
587 return(ncut);
588 }
589
590
591 double
592 pixvalue(col, x, y) /* compute pixel value */
593 COLOR col; /* returned color */
594 int x, y; /* pixel position */
595 {
596 static RAY thisray;
597
598 if (viewray(thisray.rorg, thisray.rdir, &ourview,
599 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
600 setcolor(col, 0.0, 0.0, 0.0);
601 return(0.0);
602 }
603
604 rayorigin(&thisray, NULL, PRIMARY, 1.0);
605
606 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
607
608 rayvalue(&thisray); /* trace ray */
609
610 copycolor(col, thisray.rcol); /* return color */
611
612 return(thisray.rt); /* return distance */
613 }
614
615
616 int
617 salvage(oldfile) /* salvage scanlines from killed program */
618 char *oldfile;
619 {
620 COLR *scanline;
621 FILE *fp;
622 int x, y;
623
624 if (oldfile == NULL)
625 return(0);
626
627 if ((fp = fopen(oldfile, "r")) == NULL) {
628 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
629 error(WARNING, errmsg);
630 return(0);
631 }
632 #ifdef MSDOS
633 setmode(fileno(fp), O_BINARY);
634 #endif
635 /* discard header */
636 getheader(fp, NULL, NULL);
637 /* get picture size */
638 if (!fscnresolu(&x, &y, fp)) {
639 sprintf(errmsg, "bad recover file \"%s\" - not removed",
640 oldfile);
641 error(WARNING, errmsg);
642 fclose(fp);
643 return(0);
644 }
645
646 if (x != hres || y != vres) {
647 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
648 oldfile);
649 error(USER, errmsg);
650 }
651
652 scanline = (COLR *)malloc(hres*sizeof(COLR));
653 if (scanline == NULL)
654 error(SYSTEM, "out of memory in salvage");
655 for (y = 0; y < vres; y++) {
656 if (freadcolrs(scanline, hres, fp) < 0)
657 break;
658 if (fwritecolrs(scanline, hres, stdout) < 0)
659 goto writerr;
660 }
661 if (fflush(stdout) == EOF)
662 goto writerr;
663 free((char *)scanline);
664 fclose(fp);
665 unlink(oldfile);
666 return(y);
667 writerr:
668 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
669 error(SYSTEM, errmsg);
670 }
671
672
673 int
674 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
675 register int x, y;
676 int xres, yres;
677 {
678 x -= y;
679 while (x < 0)
680 x += xres;
681 return((((x>>2)*yres + y) << 2) + (x & 3));
682 }