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