1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
6 |
|
|
7 |
|
/* |
8 |
|
* rpict.c - routines and variables for picture generation. |
9 |
– |
* |
10 |
– |
* 8/14/85 |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "ray.h" |
12 |
|
|
13 |
+ |
#include <sys/types.h> |
14 |
+ |
|
15 |
+ |
#ifndef NIX |
16 |
|
#ifdef BSD |
17 |
|
#include <sys/time.h> |
18 |
|
#include <sys/resource.h> |
19 |
|
#else |
20 |
< |
#include <signal.h> |
20 |
> |
#include <sys/times.h> |
21 |
> |
#include <unistd.h> |
22 |
|
#endif |
23 |
< |
#include <fcntl.h> |
23 |
> |
#endif |
24 |
|
|
25 |
+ |
extern time_t time(); |
26 |
+ |
|
27 |
+ |
#include <signal.h> |
28 |
+ |
|
29 |
|
#include "view.h" |
30 |
|
|
31 |
+ |
#include "resolu.h" |
32 |
+ |
|
33 |
|
#include "random.h" |
34 |
|
|
35 |
+ |
#include "paths.h" |
36 |
+ |
|
37 |
+ |
#define RFTEMPLATE "rfXXXXXX" |
38 |
+ |
|
39 |
+ |
#ifndef SIGCONT |
40 |
+ |
#define SIGCONT SIGIO |
41 |
+ |
#endif |
42 |
+ |
|
43 |
+ |
int dimlist[MAXDIM]; /* sampling dimensions */ |
44 |
+ |
int ndims = 0; /* number of sampling dimensions */ |
45 |
+ |
int samplendx; /* sample index number */ |
46 |
+ |
|
47 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
48 |
|
int hresolu = 512; /* horizontal resolution */ |
49 |
|
int vresolu = 512; /* vertical resolution */ |
50 |
< |
double pixaspect = 1.0; /* pixel aspect ratio */ |
50 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
51 |
|
|
52 |
|
int psample = 4; /* pixel sample size */ |
53 |
< |
double maxdiff = .05; /* max. difference for interpolation */ |
54 |
< |
double dstrpix = 0.67; /* square pixel distribution */ |
53 |
> |
double maxdiff = .05; /* max. difference for interpolation */ |
54 |
> |
double dstrpix = 0.67; /* square pixel distribution */ |
55 |
|
|
56 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
37 |
< |
double shadthresh = .05; /* shadow threshold */ |
38 |
< |
double shadcert = .5; /* shadow certainty */ |
56 |
> |
double mblur = 0.; /* motion blur parameter */ |
57 |
|
|
58 |
+ |
double dstrsrc = 0.0; /* square source distribution */ |
59 |
+ |
double shadthresh = .05; /* shadow threshold */ |
60 |
+ |
double shadcert = .5; /* shadow certainty */ |
61 |
+ |
int directrelay = 1; /* number of source relays */ |
62 |
+ |
int vspretest = 512; /* virtual source pretest density */ |
63 |
+ |
int directvis = 1; /* sources visible? */ |
64 |
+ |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
65 |
+ |
|
66 |
+ |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
67 |
+ |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
68 |
+ |
double seccg = 0.; /* global scattering eccentricity */ |
69 |
+ |
double ssampdist = 0.; /* scatter sampling distance */ |
70 |
+ |
|
71 |
+ |
double specthresh = .15; /* specular sampling threshold */ |
72 |
+ |
double specjitter = 1.; /* specular sampling jitter */ |
73 |
+ |
|
74 |
+ |
int backvis = 1; /* back face visibility */ |
75 |
+ |
|
76 |
|
int maxdepth = 6; /* maximum recursion depth */ |
77 |
< |
double minweight = 5e-3; /* minimum ray weight */ |
77 |
> |
double minweight = 5e-3; /* minimum ray weight */ |
78 |
|
|
79 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
80 |
< |
double ambacc = 0.2; /* ambient accuracy */ |
80 |
> |
int ambvwt = 0; /* initial weight for ambient value */ |
81 |
> |
double ambacc = 0.2; /* ambient accuracy */ |
82 |
|
int ambres = 32; /* ambient resolution */ |
83 |
|
int ambdiv = 128; /* ambient divisions */ |
84 |
|
int ambssamp = 0; /* ambient super-samples */ |
86 |
|
char *amblist[128]; /* ambient include/exclude list */ |
87 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
88 |
|
|
89 |
+ |
#ifdef MSDOS |
90 |
+ |
int ralrm = 60; /* seconds between reports */ |
91 |
+ |
#else |
92 |
|
int ralrm = 0; /* seconds between reports */ |
93 |
+ |
#endif |
94 |
|
|
95 |
< |
double pctdone = 0.0; /* percentage done */ |
95 |
> |
double pctdone = 0.0; /* percentage done */ |
96 |
|
|
97 |
< |
extern long nrays; /* number of rays traced */ |
97 |
> |
time_t tlastrept = 0L; /* time at last report */ |
98 |
|
|
99 |
< |
#define MAXDIV 32 /* maximum sample size */ |
99 |
> |
extern time_t tstart; /* starting time */ |
100 |
|
|
101 |
< |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
101 |
> |
extern unsigned long nrays; /* number of rays traced */ |
102 |
|
|
103 |
< |
double pixvalue(); |
103 |
> |
#define MAXDIV 16 /* maximum sample size */ |
104 |
|
|
105 |
+ |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
106 |
|
|
107 |
+ |
int hres, vres; /* resolution for this frame */ |
108 |
+ |
|
109 |
+ |
static VIEW lastview; /* the previous view input */ |
110 |
+ |
|
111 |
+ |
extern char *mktemp(); |
112 |
+ |
|
113 |
+ |
double pixvalue(); |
114 |
+ |
|
115 |
+ |
#ifdef NIX |
116 |
+ |
#define file_exists(f) (access(f,F_OK)==0) |
117 |
+ |
#else |
118 |
+ |
#include <sys/types.h> |
119 |
+ |
#include <sys/stat.h> |
120 |
+ |
int |
121 |
+ |
file_exists(fname) /* ordinary file exists? */ |
122 |
+ |
char *fname; |
123 |
+ |
{ |
124 |
+ |
struct stat sbuf; |
125 |
+ |
if (stat(fname, &sbuf) < 0) return(0); |
126 |
+ |
return((sbuf.st_mode & S_IFREG) != 0); |
127 |
+ |
} |
128 |
+ |
#endif |
129 |
+ |
|
130 |
+ |
|
131 |
|
quit(code) /* quit program */ |
132 |
|
int code; |
133 |
|
{ |
134 |
< |
if (code || ralrm > 0) /* report status */ |
134 |
> |
if (code) /* report status */ |
135 |
|
report(); |
136 |
< |
|
136 |
> |
headclean(); /* delete header file */ |
137 |
> |
pfclean(); /* clean up persist files */ |
138 |
|
exit(code); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
+ |
#ifndef NIX |
143 |
|
report() /* report progress */ |
144 |
|
{ |
145 |
+ |
extern char *myhostname(); |
146 |
+ |
double u, s; |
147 |
|
#ifdef BSD |
148 |
|
struct rusage rubuf; |
149 |
< |
double t; |
149 |
> |
#else |
150 |
> |
struct tms tbuf; |
151 |
> |
double period; |
152 |
> |
#endif |
153 |
|
|
154 |
+ |
tlastrept = time((time_t *)NULL); |
155 |
+ |
#ifdef BSD |
156 |
|
getrusage(RUSAGE_SELF, &rubuf); |
157 |
< |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
158 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
157 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
158 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
159 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
160 |
< |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
161 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
87 |
< |
|
88 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
89 |
< |
nrays, pctdone, t/3600.0); |
160 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
161 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
162 |
|
#else |
163 |
< |
signal(SIGALRM, report); |
164 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
163 |
> |
times(&tbuf); |
164 |
> |
#ifdef _SC_CLK_TCK |
165 |
> |
period = 1.0 / sysconf(_SC_CLK_TCK); |
166 |
> |
#else |
167 |
> |
period = 1.0 / 60.0; |
168 |
|
#endif |
169 |
+ |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; |
170 |
+ |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; |
171 |
+ |
#endif |
172 |
+ |
|
173 |
+ |
sprintf(errmsg, |
174 |
+ |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
175 |
+ |
nrays, pctdone, u/3600., s/3600., |
176 |
+ |
(tlastrept-tstart)/3600., myhostname()); |
177 |
|
eputs(errmsg); |
178 |
+ |
#ifndef BSD |
179 |
+ |
signal(SIGCONT, report); |
180 |
+ |
#endif |
181 |
+ |
} |
182 |
+ |
#else |
183 |
+ |
report() /* report progress */ |
184 |
+ |
{ |
185 |
+ |
tlastrept = time((time_t *)NULL); |
186 |
+ |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
187 |
+ |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
188 |
+ |
eputs(errmsg); |
189 |
+ |
} |
190 |
+ |
#endif |
191 |
|
|
192 |
< |
if (ralrm > 0) |
193 |
< |
alarm(ralrm); |
192 |
> |
|
193 |
> |
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
194 |
> |
int seq; |
195 |
> |
char *pout, *zout, *prvr; |
196 |
> |
/* |
197 |
> |
* If seq is greater than zero, then we will render a sequence of |
198 |
> |
* images based on view parameter strings read from the standard input. |
199 |
> |
* If pout is NULL, then all images will be sent to the standard ouput. |
200 |
> |
* If seq is greater than zero and prvr is an integer, then it is the |
201 |
> |
* frame number at which rendering should begin. Preceeding view parameter |
202 |
> |
* strings will be skipped in the input. |
203 |
> |
* If pout and prvr are the same, prvr is renamed to avoid overwriting. |
204 |
> |
* Note that pout and zout should contain %d format specifications for |
205 |
> |
* sequenced file naming. |
206 |
> |
*/ |
207 |
> |
{ |
208 |
> |
extern char *rindex(), *strncpy(), *strcat(), *strcpy(); |
209 |
> |
char fbuf[128], fbuf2[128]; |
210 |
> |
int npicts; |
211 |
> |
register char *cp; |
212 |
> |
RESOLU rs; |
213 |
> |
double pa; |
214 |
> |
/* check sampling */ |
215 |
> |
if (psample < 1) |
216 |
> |
psample = 1; |
217 |
> |
else if (psample > MAXDIV) { |
218 |
> |
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
219 |
> |
psample, MAXDIV); |
220 |
> |
error(WARNING, errmsg); |
221 |
> |
psample = MAXDIV; |
222 |
> |
} |
223 |
> |
/* get starting frame */ |
224 |
> |
if (seq <= 0) |
225 |
> |
seq = 0; |
226 |
> |
else if (prvr != NULL && isint(prvr)) { |
227 |
> |
register int rn; /* skip to specified view */ |
228 |
> |
if ((rn = atoi(prvr)) < seq) |
229 |
> |
error(USER, "recover frame less than start frame"); |
230 |
> |
if (pout == NULL) |
231 |
> |
error(USER, "missing output file specification"); |
232 |
> |
for ( ; seq < rn; seq++) |
233 |
> |
if (nextview(stdin) == EOF) |
234 |
> |
error(USER, "unexpected EOF on view input"); |
235 |
> |
setview(&ourview); |
236 |
> |
prvr = fbuf; /* mark for renaming */ |
237 |
> |
} |
238 |
> |
if (pout != NULL & prvr != NULL) { |
239 |
> |
sprintf(fbuf, pout, seq); |
240 |
> |
if (!strcmp(prvr, fbuf)) { /* rename */ |
241 |
> |
strcpy(fbuf2, fbuf); |
242 |
> |
for (cp = fbuf2; *cp; cp++) |
243 |
> |
; |
244 |
> |
while (cp > fbuf2 && !ISDIRSEP(cp[-1])) |
245 |
> |
cp--; |
246 |
> |
strcpy(cp, RFTEMPLATE); |
247 |
> |
prvr = mktemp(fbuf2); |
248 |
> |
if (rename(fbuf, prvr) < 0) |
249 |
> |
if (errno == ENOENT) { /* ghost file */ |
250 |
> |
sprintf(errmsg, |
251 |
> |
"new output file \"%s\"", |
252 |
> |
fbuf); |
253 |
> |
error(WARNING, errmsg); |
254 |
> |
prvr = NULL; |
255 |
> |
} else { /* serious error */ |
256 |
> |
sprintf(errmsg, |
257 |
> |
"cannot rename \"%s\" to \"%s\"", |
258 |
> |
fbuf, prvr); |
259 |
> |
error(SYSTEM, errmsg); |
260 |
> |
} |
261 |
> |
} |
262 |
> |
} |
263 |
> |
npicts = 0; /* render sequence */ |
264 |
> |
do { |
265 |
> |
if (seq && nextview(stdin) == EOF) |
266 |
> |
break; |
267 |
> |
pctdone = 0.0; |
268 |
> |
if (pout != NULL) { |
269 |
> |
sprintf(fbuf, pout, seq); |
270 |
> |
if (file_exists(fbuf)) { |
271 |
> |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
272 |
> |
sprintf(errmsg, |
273 |
> |
"output file \"%s\" exists", |
274 |
> |
fbuf); |
275 |
> |
error(USER, errmsg); |
276 |
> |
} |
277 |
> |
setview(&ourview); |
278 |
> |
continue; /* don't clobber */ |
279 |
> |
} |
280 |
> |
if (freopen(fbuf, "w", stdout) == NULL) { |
281 |
> |
sprintf(errmsg, |
282 |
> |
"cannot open output file \"%s\"", fbuf); |
283 |
> |
error(SYSTEM, errmsg); |
284 |
> |
} |
285 |
> |
#ifdef MSDOS |
286 |
> |
setmode(fileno(stdout), O_BINARY); |
287 |
> |
#endif |
288 |
> |
dupheader(); |
289 |
> |
} |
290 |
> |
hres = hresolu; vres = vresolu; pa = pixaspect; |
291 |
> |
if (prvr != NULL) |
292 |
> |
if (viewfile(prvr, &ourview, &rs) <= 0 |
293 |
> |
|| rs.or != PIXSTANDARD) { |
294 |
> |
sprintf(errmsg, |
295 |
> |
"cannot recover view parameters from \"%s\"", prvr); |
296 |
> |
error(WARNING, errmsg); |
297 |
> |
} else { |
298 |
> |
pa = 0.0; |
299 |
> |
hres = scanlen(&rs); |
300 |
> |
vres = numscans(&rs); |
301 |
> |
} |
302 |
> |
if ((cp = setview(&ourview)) != NULL) |
303 |
> |
error(USER, cp); |
304 |
> |
normaspect(viewaspect(&ourview), &pa, &hres, &vres); |
305 |
> |
if (seq) { |
306 |
> |
if (ralrm > 0) { |
307 |
> |
fprintf(stderr, "FRAME %d:", seq); |
308 |
> |
fprintview(&ourview, stderr); |
309 |
> |
putc('\n', stderr); |
310 |
> |
fflush(stderr); |
311 |
> |
} |
312 |
> |
printf("FRAME=%d\n", seq); |
313 |
> |
} |
314 |
> |
fputs(VIEWSTR, stdout); |
315 |
> |
fprintview(&ourview, stdout); |
316 |
> |
putchar('\n'); |
317 |
> |
if (pa < .99 || pa > 1.01) |
318 |
> |
fputaspect(pa, stdout); |
319 |
> |
fputformat(COLRFMT, stdout); |
320 |
> |
putchar('\n'); |
321 |
> |
if (zout != NULL) |
322 |
> |
sprintf(cp=fbuf, zout, seq); |
323 |
> |
else |
324 |
> |
cp = NULL; |
325 |
> |
render(cp, prvr); |
326 |
> |
prvr = NULL; |
327 |
> |
npicts++; |
328 |
> |
} while (seq++); |
329 |
> |
/* check that we did something */ |
330 |
> |
if (npicts == 0) |
331 |
> |
error(WARNING, "no output produced"); |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
+ |
nextview(fp) /* get next view from fp */ |
336 |
+ |
FILE *fp; |
337 |
+ |
{ |
338 |
+ |
char linebuf[256]; |
339 |
+ |
|
340 |
+ |
copystruct(&lastview, &ourview); |
341 |
+ |
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
342 |
+ |
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
343 |
+ |
return(0); |
344 |
+ |
return(EOF); |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
|
348 |
|
render(zfile, oldfile) /* render the scene */ |
349 |
|
char *zfile, *oldfile; |
350 |
|
{ |
104 |
– |
extern long lseek(); |
351 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
352 |
|
float *zbar[MAXDIV+1]; /* z values */ |
353 |
+ |
char *sampdens; /* previous sample density */ |
354 |
|
int ypos; /* current scanline */ |
355 |
|
int ystep; /* current y step size */ |
356 |
+ |
int hstep; /* h step size */ |
357 |
|
int zfd; |
358 |
|
COLOR *colptr; |
359 |
|
float *zptr; |
360 |
|
register int i; |
113 |
– |
/* check sampling */ |
114 |
– |
if (psample < 1) |
115 |
– |
psample = 1; |
116 |
– |
else if (psample > MAXDIV) |
117 |
– |
psample = MAXDIV; |
361 |
|
/* allocate scanlines */ |
362 |
|
for (i = 0; i <= psample; i++) { |
363 |
< |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
363 |
> |
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR)); |
364 |
|
if (scanbar[i] == NULL) |
365 |
|
goto memerr; |
366 |
|
} |
367 |
< |
/* open z file */ |
367 |
> |
hstep = (psample*140+49)/99; /* quincunx sampling */ |
368 |
> |
ystep = (psample*99+70)/140; |
369 |
> |
if (hstep > 2) { |
370 |
> |
i = hres/hstep + 2; |
371 |
> |
if ((sampdens = malloc(i)) == NULL) |
372 |
> |
goto memerr; |
373 |
> |
while (i--) |
374 |
> |
sampdens[i] = hstep; |
375 |
> |
} else |
376 |
> |
sampdens = NULL; |
377 |
> |
/* open z-file */ |
378 |
|
if (zfile != NULL) { |
379 |
|
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
380 |
< |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
380 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
381 |
|
error(SYSTEM, errmsg); |
382 |
|
} |
383 |
+ |
#ifdef MSDOS |
384 |
+ |
setmode(zfd, O_BINARY); |
385 |
+ |
#endif |
386 |
|
for (i = 0; i <= psample; i++) { |
387 |
< |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
387 |
> |
zbar[i] = (float *)malloc(hres*sizeof(float)); |
388 |
|
if (zbar[i] == NULL) |
389 |
|
goto memerr; |
390 |
|
} |
394 |
|
zbar[i] = NULL; |
395 |
|
} |
396 |
|
/* write out boundaries */ |
397 |
< |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
397 |
> |
fprtresolu(hres, vres, stdout); |
398 |
|
/* recover file and compute first */ |
399 |
|
i = salvage(oldfile); |
400 |
< |
if (zfd != -1 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
401 |
< |
error(SYSTEM, "z file seek error in render"); |
402 |
< |
ypos = vresolu-1 - i; |
403 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
404 |
< |
ystep = psample; |
400 |
> |
if (i >= vres) |
401 |
> |
goto alldone; |
402 |
> |
if (zfd != -1 && i > 0 && |
403 |
> |
lseek(zfd, (long)i*hres*sizeof(float), 0) < 0) |
404 |
> |
error(SYSTEM, "z-file seek error in render"); |
405 |
> |
pctdone = 100.0*i/vres; |
406 |
> |
if (ralrm > 0) /* report init stats */ |
407 |
> |
report(); |
408 |
> |
#ifndef BSD |
409 |
> |
else |
410 |
> |
#endif |
411 |
> |
signal(SIGCONT, report); |
412 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
413 |
> |
if (directvis) |
414 |
> |
init_drawsources(psample); |
415 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
416 |
|
/* compute scanlines */ |
417 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
151 |
– |
/* record progress */ |
152 |
– |
pctdone = 100.0*(vresolu-ypos-ystep)/vresolu; |
418 |
|
/* bottom adjust? */ |
419 |
|
if (ypos < 0) { |
420 |
|
ystep += ypos; |
427 |
|
zbar[ystep] = zbar[0]; |
428 |
|
zbar[0] = zptr; |
429 |
|
/* fill base line */ |
430 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
430 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, |
431 |
> |
hres, ypos, hstep); |
432 |
|
/* fill bar */ |
433 |
< |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
433 |
> |
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
434 |
> |
if (directvis) /* add bitty sources */ |
435 |
> |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
436 |
|
/* write it out */ |
437 |
+ |
#ifndef BSD |
438 |
+ |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
439 |
+ |
#endif |
440 |
|
for (i = ystep; i > 0; i--) { |
441 |
|
if (zfd != -1 && write(zfd, (char *)zbar[i], |
442 |
< |
hresolu*sizeof(float)) |
443 |
< |
< hresolu*sizeof(float)) |
442 |
> |
hres*sizeof(float)) |
443 |
> |
< hres*sizeof(float)) |
444 |
|
goto writerr; |
445 |
< |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
445 |
> |
if (fwritescan(scanbar[i], hres, stdout) < 0) |
446 |
|
goto writerr; |
447 |
|
} |
448 |
|
if (fflush(stdout) == EOF) |
449 |
|
goto writerr; |
450 |
+ |
/* record progress */ |
451 |
+ |
pctdone = 100.0*(vres-1-ypos)/vres; |
452 |
+ |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
453 |
+ |
report(); |
454 |
+ |
#ifndef BSD |
455 |
+ |
else |
456 |
+ |
signal(SIGCONT, report); |
457 |
+ |
#endif |
458 |
|
} |
459 |
|
/* clean up */ |
460 |
+ |
signal(SIGCONT, SIG_IGN); |
461 |
+ |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
462 |
+ |
< hres*sizeof(float)) |
463 |
+ |
goto writerr; |
464 |
+ |
fwritescan(scanbar[0], hres, stdout); |
465 |
+ |
if (fflush(stdout) == EOF) |
466 |
+ |
goto writerr; |
467 |
+ |
alldone: |
468 |
|
if (zfd != -1) { |
469 |
< |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
183 |
< |
< hresolu*sizeof(float)) |
469 |
> |
if (close(zfd) == -1) |
470 |
|
goto writerr; |
185 |
– |
close(zfd); |
471 |
|
for (i = 0; i <= psample; i++) |
472 |
|
free((char *)zbar[i]); |
473 |
|
} |
189 |
– |
fwritescan(scanbar[0], hresolu, stdout); |
190 |
– |
if (fflush(stdout) == EOF) |
191 |
– |
goto writerr; |
474 |
|
for (i = 0; i <= psample; i++) |
475 |
|
free((char *)scanbar[i]); |
476 |
+ |
if (sampdens != NULL) |
477 |
+ |
free(sampdens); |
478 |
|
pctdone = 100.0; |
479 |
+ |
if (ralrm > 0) |
480 |
+ |
report(); |
481 |
+ |
signal(SIGCONT, SIG_DFL); |
482 |
|
return; |
483 |
|
writerr: |
484 |
|
error(SYSTEM, "write error in render"); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
< |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
491 |
< |
register COLOR *scanline; |
492 |
< |
register float *zline; |
490 |
> |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
491 |
> |
register COLOR *scanline; |
492 |
> |
register float *zline; |
493 |
> |
register char *sd; |
494 |
|
int xres, y, xstep; |
495 |
|
{ |
496 |
< |
int b = xstep; |
497 |
< |
double z; |
496 |
> |
static int nc = 0; /* number of calls */ |
497 |
> |
int bl = xstep, b = xstep; |
498 |
> |
double z; |
499 |
|
register int i; |
500 |
< |
|
500 |
> |
|
501 |
|
z = pixvalue(scanline[0], 0, y); |
502 |
|
if (zline) zline[0] = z; |
503 |
< |
|
504 |
< |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
503 |
> |
/* zig-zag start for quincunx pattern */ |
504 |
> |
for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) { |
505 |
|
if (i >= xres) { |
506 |
|
xstep += xres-1-i; |
507 |
|
i = xres-1; |
508 |
|
} |
509 |
|
z = pixvalue(scanline[i], i, y); |
510 |
|
if (zline) zline[i] = z; |
511 |
< |
|
512 |
< |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
513 |
< |
i-xstep, y, xstep, 0, b/2); |
511 |
> |
if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1]; |
512 |
> |
if (i <= xstep) |
513 |
> |
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
514 |
> |
else |
515 |
> |
b = fillsample(scanline+i-xstep, |
516 |
> |
zline ? zline+i-xstep : (float *)NULL, |
517 |
> |
i-xstep, y, xstep, 0, b/2); |
518 |
> |
if (sd) *sd++ = nc & 1 ? bl : b; |
519 |
> |
bl = b; |
520 |
|
} |
521 |
+ |
if (sd && nc & 1) *sd = bl; |
522 |
|
} |
523 |
|
|
524 |
|
|
525 |
|
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
526 |
< |
register COLOR *scanbar[]; |
527 |
< |
register float *zbar[]; |
526 |
> |
register COLOR *scanbar[]; |
527 |
> |
register float *zbar[]; |
528 |
|
int xres, y, ysize; |
529 |
|
{ |
530 |
|
COLOR vline[MAXDIV+1]; |
531 |
|
float zline[MAXDIV+1]; |
532 |
|
int b = ysize; |
237 |
– |
double z; |
533 |
|
register int i, j; |
534 |
< |
|
534 |
> |
|
535 |
|
for (i = 0; i < xres; i++) { |
241 |
– |
|
536 |
|
copycolor(vline[0], scanbar[0][i]); |
537 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
538 |
|
if (zbar[0]) { |
539 |
|
zline[0] = zbar[0][i]; |
540 |
|
zline[ysize] = zbar[ysize][i]; |
541 |
|
} |
542 |
< |
|
249 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
542 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
543 |
|
i, y, 0, ysize, b/2); |
544 |
< |
|
544 |
> |
|
545 |
|
for (j = 1; j < ysize; j++) |
546 |
|
copycolor(scanbar[j][i], vline[j]); |
547 |
|
if (zbar[0]) |
552 |
|
|
553 |
|
|
554 |
|
int |
555 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
556 |
< |
register COLOR *colline; |
557 |
< |
register float *zline; |
555 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
556 |
> |
register COLOR *colline; |
557 |
> |
register float *zline; |
558 |
|
int x, y; |
559 |
|
int xlen, ylen; |
560 |
|
int b; |
561 |
|
{ |
562 |
< |
extern double fabs(); |
563 |
< |
double ratio; |
271 |
< |
double z; |
562 |
> |
double ratio; |
563 |
> |
double z; |
564 |
|
COLOR ctmp; |
565 |
|
int ncut; |
566 |
|
register int len; |
567 |
< |
|
567 |
> |
|
568 |
|
if (xlen > 0) /* x or y length is zero */ |
569 |
|
len = xlen; |
570 |
|
else |
571 |
|
len = ylen; |
572 |
< |
|
572 |
> |
|
573 |
|
if (len <= 1) /* limit recursion */ |
574 |
|
return(0); |
575 |
< |
|
576 |
< |
if (b > 0 |
577 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
575 |
> |
|
576 |
> |
if (b > 0 || |
577 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
578 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
579 |
< |
|
579 |
> |
|
580 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
581 |
|
if (zline) zline[len>>1] = z; |
582 |
|
ncut = 1; |
291 |
– |
|
583 |
|
} else { /* interpolate */ |
293 |
– |
|
584 |
|
copycolor(colline[len>>1], colline[len]); |
585 |
|
ratio = (double)(len>>1) / len; |
586 |
|
scalecolor(colline[len>>1], ratio); |
594 |
|
} |
595 |
|
/* recurse */ |
596 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
597 |
< |
|
598 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
597 |
> |
|
598 |
> |
ncut += fillsample(colline+(len>>1), |
599 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
600 |
|
x+(xlen>>1), y+(ylen>>1), |
601 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
602 |
|
|
609 |
|
COLOR col; /* returned color */ |
610 |
|
int x, y; /* pixel position */ |
611 |
|
{ |
612 |
< |
static RAY thisray; /* our ray for this pixel */ |
612 |
> |
RAY thisray; |
613 |
> |
FVECT lorg, ldir; |
614 |
> |
double hpos, vpos, lmax, d; |
615 |
> |
/* compute view ray */ |
616 |
> |
hpos = (x+pixjitter())/hres; |
617 |
> |
vpos = (y+pixjitter())/vres; |
618 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
619 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
620 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
621 |
> |
return(0.0); |
622 |
> |
} |
623 |
|
|
624 |
< |
viewray(thisray.rorg, thisray.rdir, &ourview, |
324 |
< |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu); |
624 |
> |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
625 |
|
|
626 |
+ |
/* optional motion blur */ |
627 |
+ |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
628 |
+ |
&lastview, hpos, vpos)) >= -FTINY) { |
629 |
+ |
register int i; |
630 |
+ |
register double d = mblur*(.5-urand(281+samplendx)); |
631 |
+ |
|
632 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
633 |
+ |
for (i = 3; i--; ) { |
634 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
635 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
636 |
+ |
} |
637 |
+ |
if (normalize(thisray.rdir) == 0.0) |
638 |
+ |
return(0.0); |
639 |
+ |
} |
640 |
+ |
|
641 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
642 |
< |
|
642 |
> |
|
643 |
|
rayvalue(&thisray); /* trace ray */ |
644 |
|
|
645 |
|
copycolor(col, thisray.rcol); /* return color */ |
646 |
< |
|
647 |
< |
return(thisray.rot); /* return distance */ |
646 |
> |
|
647 |
> |
return(thisray.rt); /* return distance */ |
648 |
|
} |
649 |
|
|
650 |
|
|
657 |
|
int x, y; |
658 |
|
|
659 |
|
if (oldfile == NULL) |
660 |
< |
return(0); |
661 |
< |
|
660 |
> |
goto gotzip; |
661 |
> |
|
662 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
663 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
664 |
|
error(WARNING, errmsg); |
665 |
< |
return(0); |
665 |
> |
goto gotzip; |
666 |
|
} |
667 |
+ |
#ifdef MSDOS |
668 |
+ |
setmode(fileno(fp), O_BINARY); |
669 |
+ |
#endif |
670 |
|
/* discard header */ |
671 |
< |
getheader(fp, NULL); |
671 |
> |
getheader(fp, NULL, NULL); |
672 |
|
/* get picture size */ |
673 |
< |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
674 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
673 |
> |
if (!fscnresolu(&x, &y, fp)) { |
674 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
675 |
> |
oldfile); |
676 |
|
error(WARNING, errmsg); |
677 |
|
fclose(fp); |
678 |
< |
return(0); |
678 |
> |
goto gotzip; |
679 |
|
} |
680 |
|
|
681 |
< |
if (x != hresolu || y != vresolu) { |
681 |
> |
if (x != hres || y != vres) { |
682 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
683 |
|
oldfile); |
684 |
|
error(USER, errmsg); |
685 |
|
} |
686 |
|
|
687 |
< |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
687 |
> |
scanline = (COLR *)malloc(hres*sizeof(COLR)); |
688 |
|
if (scanline == NULL) |
689 |
|
error(SYSTEM, "out of memory in salvage"); |
690 |
< |
for (y = 0; y < vresolu; y++) { |
691 |
< |
if (freadcolrs(scanline, hresolu, fp) < 0) |
690 |
> |
for (y = 0; y < vres; y++) { |
691 |
> |
if (freadcolrs(scanline, hres, fp) < 0) |
692 |
|
break; |
693 |
< |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
693 |
> |
if (fwritecolrs(scanline, hres, stdout) < 0) |
694 |
|
goto writerr; |
695 |
|
} |
696 |
|
if (fflush(stdout) == EOF) |
699 |
|
fclose(fp); |
700 |
|
unlink(oldfile); |
701 |
|
return(y); |
702 |
+ |
gotzip: |
703 |
+ |
if (fflush(stdout) == EOF) |
704 |
+ |
error(SYSTEM, "error writing picture header"); |
705 |
+ |
return(0); |
706 |
|
writerr: |
707 |
< |
error(SYSTEM, "write error in salvage"); |
707 |
> |
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
708 |
> |
error(SYSTEM, errmsg); |
709 |
> |
} |
710 |
> |
|
711 |
> |
|
712 |
> |
int |
713 |
> |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
714 |
> |
register int x, y; |
715 |
> |
int xres, yres; |
716 |
> |
{ |
717 |
> |
x -= y; |
718 |
> |
while (x < 0) |
719 |
> |
x += xres; |
720 |
> |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
721 |
|
} |