ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.23
Committed: Fri Jan 29 12:15:40 1993 UTC (31 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +1 -0 lines
Log Message:
added final instruction to render() to set SIGCONT to default action

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