ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.29
Committed: Wed Aug 11 09:46:15 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +2 -1 lines
Log Message:
improved error reporting on bad recover files

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