ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.11
Committed: Sun Aug 9 20:28:41 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +4 -2 lines
Log Message:
added view parameters to frame reporting

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