ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.50
Committed: Mon Apr 13 14:01:08 1998 UTC (26 years ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.49: +2 -0 lines
Log Message:
added missing calls to setview()

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