ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.19
Committed: Thu Nov 12 10:11:53 1992 UTC (31 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +1 -1 lines
Log Message:
fixed call to getheader() to pass 3 arguments always (DOS bug fix)

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