ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.31
Committed: Thu Nov 18 09:22:55 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +8 -6 lines
Log Message:
fixed time declaration and use

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