ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.25
Committed: Fri May 28 18:59:50 1993 UTC (30 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.24: +1 -0 lines
Log Message:
reinitialized pctdone in rpict()

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