ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.32
Committed: Mon Nov 22 09:34:15 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +16 -11 lines
Log Message:
compatibility fixes

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 <limits.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 #define hostname nambuf.sysname
144 #endif
145
146 tlastrept = time((time_t *)NULL);
147 #ifdef BSD
148 getrusage(RUSAGE_SELF, &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 getrusage(RUSAGE_CHILDREN, &rubuf);
152 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
153 s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
154 gethostname(hostname, sizeof(hostname));
155 #else
156 times(&tbuf);
157 u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK;
158 s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK;
159 uname(&nambuf);
160 #endif
161
162 sprintf(errmsg,
163 "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
164 nrays, pctdone, u/3600., s/3600.,
165 (tlastrept-tstart)/3600., hostname);
166 eputs(errmsg);
167 #undef hostname
168 }
169 #else
170 report() /* report progress */
171 {
172 tlastrept = time((time_t *)NULL);
173 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
174 nrays, pctdone, (tlastrept-tstart)/3600.0);
175 eputs(errmsg);
176 signal(SIGCONT, report);
177 }
178 #endif
179
180
181 rpict(seq, pout, zout, prvr) /* generate image(s) */
182 int seq;
183 char *pout, *zout, *prvr;
184 /*
185 * If seq is greater than zero, then we will render a sequence of
186 * images based on view parameter strings read from the standard input.
187 * If pout is NULL, then all images will be sent to the standard ouput.
188 * If seq is greater than zero and prvr is an integer, then it is the
189 * frame number at which rendering should begin. Preceeding view parameter
190 * strings will be skipped in the input.
191 * If pout and prvr are the same, prvr is renamed to avoid overwriting.
192 * Note that pout and zout should contain %d format specifications for
193 * sequenced file naming.
194 */
195 {
196 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
197 char fbuf[128], fbuf2[128];
198 int npicts;
199 register char *cp;
200 RESOLU rs;
201 double pa;
202 /* check sampling */
203 if (psample < 1)
204 psample = 1;
205 else if (psample > MAXDIV) {
206 sprintf(errmsg, "pixel sampling reduced from %d to %d",
207 psample, MAXDIV);
208 error(WARNING, errmsg);
209 psample = MAXDIV;
210 }
211 /* get starting frame */
212 if (seq <= 0)
213 seq = 0;
214 else if (prvr != NULL && isint(prvr)) {
215 register int rn; /* skip to specified view */
216 if ((rn = atoi(prvr)) < seq)
217 error(USER, "recover frame less than start frame");
218 if (pout == NULL)
219 error(USER, "missing output file specification");
220 for ( ; seq < rn; seq++)
221 if (nextview(stdin) == EOF)
222 error(USER, "unexpected EOF on view input");
223 prvr = fbuf; /* mark for renaming */
224 }
225 if (pout != NULL & prvr != NULL) {
226 sprintf(fbuf, pout, seq);
227 if (!strcmp(prvr, fbuf)) { /* rename */
228 strcpy(fbuf2, fbuf);
229 for (cp = fbuf2; *cp; cp++)
230 ;
231 while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
232 cp--;
233 strcpy(cp, RFTEMPLATE);
234 prvr = mktemp(fbuf2);
235 if (rename(fbuf, prvr) < 0)
236 if (errno == ENOENT) { /* ghost file */
237 sprintf(errmsg,
238 "new output file \"%s\"",
239 fbuf);
240 error(WARNING, errmsg);
241 prvr = NULL;
242 } else { /* serious error */
243 sprintf(errmsg,
244 "cannot rename \"%s\" to \"%s\"",
245 fbuf, prvr);
246 error(SYSTEM, errmsg);
247 }
248 }
249 }
250 npicts = 0; /* render sequence */
251 do {
252 if (seq && nextview(stdin) == EOF)
253 break;
254 pctdone = 0.0;
255 if (pout != NULL) {
256 sprintf(fbuf, pout, seq);
257 if (file_exists(fbuf)) {
258 if (prvr != NULL || !strcmp(fbuf, pout)) {
259 sprintf(errmsg,
260 "output file \"%s\" exists",
261 fbuf);
262 error(USER, errmsg);
263 }
264 continue; /* don't clobber */
265 }
266 if (freopen(fbuf, "w", stdout) == NULL) {
267 sprintf(errmsg,
268 "cannot open output file \"%s\"", fbuf);
269 error(SYSTEM, errmsg);
270 }
271 #ifdef MSDOS
272 setmode(fileno(stdout), O_BINARY);
273 #endif
274 dupheader();
275 }
276 hres = hresolu; vres = vresolu; pa = pixaspect;
277 if (prvr != NULL)
278 if (viewfile(prvr, &ourview, &rs) <= 0
279 || rs.or != PIXSTANDARD) {
280 sprintf(errmsg,
281 "cannot recover view parameters from \"%s\"", prvr);
282 error(WARNING, errmsg);
283 } else {
284 pa = 0.0;
285 hres = scanlen(&rs);
286 vres = numscans(&rs);
287 }
288 if ((cp = setview(&ourview)) != NULL)
289 error(USER, cp);
290 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
291 if (seq) {
292 if (ralrm > 0) {
293 fprintf(stderr, "FRAME %d:", seq);
294 fprintview(&ourview, stderr);
295 putc('\n', stderr);
296 fflush(stderr);
297 }
298 printf("FRAME=%d\n", seq);
299 }
300 fputs(VIEWSTR, stdout);
301 fprintview(&ourview, stdout);
302 putchar('\n');
303 if (pa < .99 || pa > 1.01)
304 fputaspect(pa, stdout);
305 fputformat(COLRFMT, stdout);
306 putchar('\n');
307 if (zout != NULL)
308 sprintf(cp=fbuf, zout, seq);
309 else
310 cp = NULL;
311 render(cp, prvr);
312 prvr = NULL;
313 npicts++;
314 } while (seq++);
315 /* check that we did something */
316 if (npicts == 0)
317 error(WARNING, "no output produced");
318 }
319
320
321 nextview(fp) /* get next view from fp */
322 FILE *fp;
323 {
324 char linebuf[256];
325
326 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
327 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
328 return(0);
329 return(EOF);
330 }
331
332
333 render(zfile, oldfile) /* render the scene */
334 char *zfile, *oldfile;
335 {
336 extern long lseek();
337 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
338 float *zbar[MAXDIV+1]; /* z values */
339 char *sampdens; /* previous sample density */
340 int ypos; /* current scanline */
341 int ystep; /* current y step size */
342 int hstep; /* h step size */
343 int zfd;
344 COLOR *colptr;
345 float *zptr;
346 register int i;
347 /* allocate scanlines */
348 for (i = 0; i <= psample; i++) {
349 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
350 if (scanbar[i] == NULL)
351 goto memerr;
352 }
353 hstep = (psample*140+49)/99; /* quincunx sampling */
354 ystep = (psample*99+70)/140;
355 if (hstep > 2) {
356 i = hres/hstep + 2;
357 if ((sampdens = malloc(i)) == NULL)
358 goto memerr;
359 while (i--)
360 sampdens[i] = hstep;
361 } else
362 sampdens = NULL;
363 /* open z-file */
364 if (zfile != NULL) {
365 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
366 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
367 error(SYSTEM, errmsg);
368 }
369 #ifdef MSDOS
370 setmode(zfd, O_BINARY);
371 #endif
372 for (i = 0; i <= psample; i++) {
373 zbar[i] = (float *)malloc(hres*sizeof(float));
374 if (zbar[i] == NULL)
375 goto memerr;
376 }
377 } else {
378 zfd = -1;
379 for (i = 0; i <= psample; i++)
380 zbar[i] = NULL;
381 }
382 /* write out boundaries */
383 fprtresolu(hres, vres, stdout);
384 /* recover file and compute first */
385 i = salvage(oldfile);
386 if (zfd != -1 && i > 0 &&
387 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
388 error(SYSTEM, "z-file seek error in render");
389 pctdone = 100.0*i/vres;
390 if (ralrm > 0) /* report init stats */
391 report();
392 #ifndef BSD
393 else
394 #endif
395 signal(SIGCONT, report);
396 ypos = vres-1 - i;
397 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
398 /* compute scanlines */
399 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
400 /* bottom adjust? */
401 if (ypos < 0) {
402 ystep += ypos;
403 ypos = 0;
404 }
405 colptr = scanbar[ystep]; /* move base to top */
406 scanbar[ystep] = scanbar[0];
407 scanbar[0] = colptr;
408 zptr = zbar[ystep];
409 zbar[ystep] = zbar[0];
410 zbar[0] = zptr;
411 /* fill base line */
412 fillscanline(scanbar[0], zbar[0], sampdens,
413 hres, ypos, hstep);
414 /* fill bar */
415 fillscanbar(scanbar, zbar, hres, ypos, ystep);
416 /* write it out */
417 #ifndef BSD
418 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
419 #endif
420 for (i = ystep; i > 0; i--) {
421 if (zfd != -1 && write(zfd, (char *)zbar[i],
422 hres*sizeof(float))
423 < hres*sizeof(float))
424 goto writerr;
425 if (fwritescan(scanbar[i], hres, stdout) < 0)
426 goto writerr;
427 }
428 if (fflush(stdout) == EOF)
429 goto writerr;
430 /* record progress */
431 pctdone = 100.0*(vres-1-ypos)/vres;
432 if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
433 report();
434 #ifndef BSD
435 else
436 signal(SIGCONT, report);
437 #endif
438 }
439 /* clean up */
440 signal(SIGCONT, SIG_IGN);
441 if (zfd != -1) {
442 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
443 < hres*sizeof(float))
444 goto writerr;
445 if (close(zfd) == -1)
446 goto writerr;
447 for (i = 0; i <= psample; i++)
448 free((char *)zbar[i]);
449 }
450 fwritescan(scanbar[0], hres, stdout);
451 if (fflush(stdout) == EOF)
452 goto writerr;
453 for (i = 0; i <= psample; i++)
454 free((char *)scanbar[i]);
455 if (sampdens != NULL)
456 free(sampdens);
457 pctdone = 100.0;
458 if (ralrm > 0)
459 report();
460 signal(SIGCONT, SIG_DFL);
461 return;
462 writerr:
463 error(SYSTEM, "write error in render");
464 memerr:
465 error(SYSTEM, "out of memory in render");
466 }
467
468
469 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
470 register COLOR *scanline;
471 register float *zline;
472 register char *sd;
473 int xres, y, xstep;
474 {
475 static int nc = 0; /* number of calls */
476 int bl = xstep, b = xstep;
477 double z;
478 register int i;
479
480 z = pixvalue(scanline[0], 0, y);
481 if (zline) zline[0] = z;
482 /* zig-zag start for quincunx pattern */
483 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
484 if (i >= xres) {
485 xstep += xres-1-i;
486 i = xres-1;
487 }
488 z = pixvalue(scanline[i], i, y);
489 if (zline) zline[i] = z;
490 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
491 if (i <= xstep)
492 b = fillsample(scanline, zline, 0, y, i, 0, b/2);
493 else
494 b = fillsample(scanline+i-xstep,
495 zline ? zline+i-xstep : NULL,
496 i-xstep, y, xstep, 0, b/2);
497 if (sd) *sd++ = nc & 1 ? bl : b;
498 bl = b;
499 }
500 if (sd && nc & 1) *sd = bl;
501 }
502
503
504 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
505 register COLOR *scanbar[];
506 register float *zbar[];
507 int xres, y, ysize;
508 {
509 COLOR vline[MAXDIV+1];
510 float zline[MAXDIV+1];
511 int b = ysize;
512 register int i, j;
513
514 for (i = 0; i < xres; i++) {
515
516 copycolor(vline[0], scanbar[0][i]);
517 copycolor(vline[ysize], scanbar[ysize][i]);
518 if (zbar[0]) {
519 zline[0] = zbar[0][i];
520 zline[ysize] = zbar[ysize][i];
521 }
522
523 b = fillsample(vline, zbar[0] ? zline : NULL,
524 i, y, 0, ysize, b/2);
525
526 for (j = 1; j < ysize; j++)
527 copycolor(scanbar[j][i], vline[j]);
528 if (zbar[0])
529 for (j = 1; j < ysize; j++)
530 zbar[j][i] = zline[j];
531 }
532 }
533
534
535 int
536 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
537 register COLOR *colline;
538 register float *zline;
539 int x, y;
540 int xlen, ylen;
541 int b;
542 {
543 double ratio;
544 double z;
545 COLOR ctmp;
546 int ncut;
547 register int len;
548
549 if (xlen > 0) /* x or y length is zero */
550 len = xlen;
551 else
552 len = ylen;
553
554 if (len <= 1) /* limit recursion */
555 return(0);
556
557 if (b > 0
558 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
559 || bigdiff(colline[0], colline[len], maxdiff)) {
560
561 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
562 if (zline) zline[len>>1] = z;
563 ncut = 1;
564
565 } else { /* interpolate */
566
567 copycolor(colline[len>>1], colline[len]);
568 ratio = (double)(len>>1) / len;
569 scalecolor(colline[len>>1], ratio);
570 if (zline) zline[len>>1] = zline[len] * ratio;
571 ratio = 1.0 - ratio;
572 copycolor(ctmp, colline[0]);
573 scalecolor(ctmp, ratio);
574 addcolor(colline[len>>1], ctmp);
575 if (zline) zline[len>>1] += zline[0] * ratio;
576 ncut = 0;
577 }
578 /* recurse */
579 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
580
581 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
582 x+(xlen>>1), y+(ylen>>1),
583 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
584
585 return(ncut);
586 }
587
588
589 double
590 pixvalue(col, x, y) /* compute pixel value */
591 COLOR col; /* returned color */
592 int x, y; /* pixel position */
593 {
594 static RAY thisray;
595
596 if (viewray(thisray.rorg, thisray.rdir, &ourview,
597 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
598 setcolor(col, 0.0, 0.0, 0.0);
599 return(0.0);
600 }
601
602 rayorigin(&thisray, NULL, PRIMARY, 1.0);
603
604 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
605
606 rayvalue(&thisray); /* trace ray */
607
608 copycolor(col, thisray.rcol); /* return color */
609
610 return(thisray.rt); /* return distance */
611 }
612
613
614 int
615 salvage(oldfile) /* salvage scanlines from killed program */
616 char *oldfile;
617 {
618 COLR *scanline;
619 FILE *fp;
620 int x, y;
621
622 if (oldfile == NULL)
623 return(0);
624
625 if ((fp = fopen(oldfile, "r")) == NULL) {
626 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
627 error(WARNING, errmsg);
628 return(0);
629 }
630 #ifdef MSDOS
631 setmode(fileno(fp), O_BINARY);
632 #endif
633 /* discard header */
634 getheader(fp, NULL, NULL);
635 /* get picture size */
636 if (!fscnresolu(&x, &y, fp)) {
637 sprintf(errmsg, "bad recover file \"%s\" - not removed",
638 oldfile);
639 error(WARNING, errmsg);
640 fclose(fp);
641 return(0);
642 }
643
644 if (x != hres || y != vres) {
645 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
646 oldfile);
647 error(USER, errmsg);
648 }
649
650 scanline = (COLR *)malloc(hres*sizeof(COLR));
651 if (scanline == NULL)
652 error(SYSTEM, "out of memory in salvage");
653 for (y = 0; y < vres; y++) {
654 if (freadcolrs(scanline, hres, fp) < 0)
655 break;
656 if (fwritecolrs(scanline, hres, stdout) < 0)
657 goto writerr;
658 }
659 if (fflush(stdout) == EOF)
660 goto writerr;
661 free((char *)scanline);
662 fclose(fp);
663 unlink(oldfile);
664 return(y);
665 writerr:
666 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
667 error(SYSTEM, errmsg);
668 }
669
670
671 int
672 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
673 register int x, y;
674 int xres, yres;
675 {
676 x -= y;
677 while (x < 0)
678 x += xres;
679 return((((x>>2)*yres + y) << 2) + (x & 3));
680 }