ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.32
Committed: Mon Nov 22 09:34:15 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +16 -11 lines
Log Message:
compatibility fixes

File Contents

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