ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.12
Committed: Tue Sep 8 15:35:16 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +3 -1 lines
Log Message:
added paths.h for definition of temporary file template

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