ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.35
Committed: Wed Nov 24 14:28:08 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +3 -1 lines
Log Message:
fixed error in SIGCONT catching for System V

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