ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.17
Committed: Thu Oct 15 21:13:17 1992 UTC (31 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +12 -0 lines
Log Message:
added setmode(fd,O_BINARY) where needed for MSDOS

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