ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.48
Committed: Mon Oct 20 16:46:37 1997 UTC (26 years, 6 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.47: +4 -3 lines
Log Message:
added test of directvis before call to drawsources()

File Contents

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