ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.13
Committed: Tue Sep 8 20:56:20 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -0 lines
Log Message:
put back in final report() call

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