ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.40
Committed: Wed Dec 21 09:51:56 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.39: +2 -0 lines
Log Message:
added -bv option for back face visibility (normally on)

File Contents

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