ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.15
Committed: Fri Oct 2 16:19:10 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +0 -1 lines
Log Message:
Removed problematic math function declarations

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