ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.16
Committed: Tue Oct 6 12:30:07 1992 UTC (31 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +4 -0 lines
Log Message:
Changes for 32-bit PC port

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