ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.35
Committed: Wed Nov 24 14:28:08 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +3 -1 lines
Log Message:
fixed error in SIGCONT catching for System V

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