1 |
< |
/* Copyright (c) 1991 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 */ |
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 */ |
41 |
< |
double shadthresh = .05; /* shadow threshold */ |
42 |
< |
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 |
< |
long tlastrept = 0L; /* time at last report */ |
97 |
> |
time_t tlastrept = 0L; /* time at last report */ |
98 |
|
|
99 |
< |
extern long time(); |
63 |
< |
extern long tstart; /* starting time */ |
99 |
> |
extern time_t tstart; /* starting time */ |
100 |
|
|
101 |
< |
extern long nrays; /* number of rays traced */ |
101 |
> |
extern unsigned long nrays; /* number of rays traced */ |
102 |
|
|
103 |
< |
#define MAXDIV 15 /* maximum sample size */ |
103 |
> |
#define MAXDIV 16 /* maximum sample size */ |
104 |
|
|
105 |
< |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
105 |
> |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
106 |
|
|
107 |
< |
double pixvalue(); |
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 |
< |
#ifdef BSD |
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; |
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 |
> |
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, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
174 |
< |
nrays, pctdone, t/3600.0); |
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 |
< |
tlastrept = time((long *)0); |
178 |
> |
#ifndef BSD |
179 |
> |
signal(SIGCONT, report); |
180 |
> |
#endif |
181 |
|
} |
182 |
|
#else |
183 |
|
report() /* report progress */ |
184 |
|
{ |
185 |
< |
signal(SIGALRM, report); |
186 |
< |
tlastrept = time((long *)0); |
107 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
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 |
|
|
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 |
+ |
prvr = fbuf; /* mark for renaming */ |
236 |
+ |
} |
237 |
+ |
if (pout != NULL & prvr != NULL) { |
238 |
+ |
sprintf(fbuf, pout, seq); |
239 |
+ |
if (!strcmp(prvr, fbuf)) { /* rename */ |
240 |
+ |
strcpy(fbuf2, fbuf); |
241 |
+ |
for (cp = fbuf2; *cp; cp++) |
242 |
+ |
; |
243 |
+ |
while (cp > fbuf2 && !ISDIRSEP(cp[-1])) |
244 |
+ |
cp--; |
245 |
+ |
strcpy(cp, RFTEMPLATE); |
246 |
+ |
prvr = mktemp(fbuf2); |
247 |
+ |
if (rename(fbuf, prvr) < 0) |
248 |
+ |
if (errno == ENOENT) { /* ghost file */ |
249 |
+ |
sprintf(errmsg, |
250 |
+ |
"new output file \"%s\"", |
251 |
+ |
fbuf); |
252 |
+ |
error(WARNING, errmsg); |
253 |
+ |
prvr = NULL; |
254 |
+ |
} else { /* serious error */ |
255 |
+ |
sprintf(errmsg, |
256 |
+ |
"cannot rename \"%s\" to \"%s\"", |
257 |
+ |
fbuf, prvr); |
258 |
+ |
error(SYSTEM, errmsg); |
259 |
+ |
} |
260 |
+ |
} |
261 |
+ |
} |
262 |
+ |
npicts = 0; /* render sequence */ |
263 |
+ |
do { |
264 |
+ |
if (seq && nextview(stdin) == EOF) |
265 |
+ |
break; |
266 |
+ |
pctdone = 0.0; |
267 |
+ |
if (pout != NULL) { |
268 |
+ |
sprintf(fbuf, pout, seq); |
269 |
+ |
if (file_exists(fbuf)) { |
270 |
+ |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
271 |
+ |
sprintf(errmsg, |
272 |
+ |
"output file \"%s\" exists", |
273 |
+ |
fbuf); |
274 |
+ |
error(USER, errmsg); |
275 |
+ |
} |
276 |
+ |
continue; /* don't clobber */ |
277 |
+ |
} |
278 |
+ |
if (freopen(fbuf, "w", stdout) == NULL) { |
279 |
+ |
sprintf(errmsg, |
280 |
+ |
"cannot open output file \"%s\"", fbuf); |
281 |
+ |
error(SYSTEM, errmsg); |
282 |
+ |
} |
283 |
+ |
#ifdef MSDOS |
284 |
+ |
setmode(fileno(stdout), O_BINARY); |
285 |
+ |
#endif |
286 |
+ |
dupheader(); |
287 |
+ |
} |
288 |
+ |
hres = hresolu; vres = vresolu; pa = pixaspect; |
289 |
+ |
if (prvr != NULL) |
290 |
+ |
if (viewfile(prvr, &ourview, &rs) <= 0 |
291 |
+ |
|| rs.or != PIXSTANDARD) { |
292 |
+ |
sprintf(errmsg, |
293 |
+ |
"cannot recover view parameters from \"%s\"", prvr); |
294 |
+ |
error(WARNING, errmsg); |
295 |
+ |
} else { |
296 |
+ |
pa = 0.0; |
297 |
+ |
hres = scanlen(&rs); |
298 |
+ |
vres = numscans(&rs); |
299 |
+ |
} |
300 |
+ |
if ((cp = setview(&ourview)) != NULL) |
301 |
+ |
error(USER, cp); |
302 |
+ |
normaspect(viewaspect(&ourview), &pa, &hres, &vres); |
303 |
+ |
if (seq) { |
304 |
+ |
if (ralrm > 0) { |
305 |
+ |
fprintf(stderr, "FRAME %d:", seq); |
306 |
+ |
fprintview(&ourview, stderr); |
307 |
+ |
putc('\n', stderr); |
308 |
+ |
fflush(stderr); |
309 |
+ |
} |
310 |
+ |
printf("FRAME=%d\n", seq); |
311 |
+ |
} |
312 |
+ |
fputs(VIEWSTR, stdout); |
313 |
+ |
fprintview(&ourview, stdout); |
314 |
+ |
putchar('\n'); |
315 |
+ |
if (pa < .99 || pa > 1.01) |
316 |
+ |
fputaspect(pa, stdout); |
317 |
+ |
fputformat(COLRFMT, stdout); |
318 |
+ |
putchar('\n'); |
319 |
+ |
if (zout != NULL) |
320 |
+ |
sprintf(cp=fbuf, zout, seq); |
321 |
+ |
else |
322 |
+ |
cp = NULL; |
323 |
+ |
render(cp, prvr); |
324 |
+ |
prvr = NULL; |
325 |
+ |
npicts++; |
326 |
+ |
} while (seq++); |
327 |
+ |
/* check that we did something */ |
328 |
+ |
if (npicts == 0) |
329 |
+ |
error(WARNING, "no output produced"); |
330 |
+ |
} |
331 |
+ |
|
332 |
+ |
|
333 |
+ |
nextview(fp) /* get next view from fp */ |
334 |
+ |
FILE *fp; |
335 |
+ |
{ |
336 |
+ |
char linebuf[256]; |
337 |
+ |
|
338 |
+ |
copystruct(&lastview, &ourview); |
339 |
+ |
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) |
340 |
+ |
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) |
341 |
+ |
return(0); |
342 |
+ |
return(EOF); |
343 |
+ |
} |
344 |
+ |
|
345 |
+ |
|
346 |
|
render(zfile, oldfile) /* render the scene */ |
347 |
|
char *zfile, *oldfile; |
348 |
|
{ |
349 |
|
extern long lseek(); |
350 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
351 |
|
float *zbar[MAXDIV+1]; /* z values */ |
352 |
+ |
char *sampdens; /* previous sample density */ |
353 |
|
int ypos; /* current scanline */ |
354 |
|
int ystep; /* current y step size */ |
355 |
+ |
int hstep; /* h step size */ |
356 |
|
int zfd; |
357 |
|
COLOR *colptr; |
358 |
|
float *zptr; |
359 |
|
register int i; |
126 |
– |
/* check sampling */ |
127 |
– |
if (psample < 1) |
128 |
– |
psample = 1; |
129 |
– |
else if (psample > MAXDIV) { |
130 |
– |
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
131 |
– |
psample, MAXDIV); |
132 |
– |
error(WARNING, errmsg); |
133 |
– |
psample = MAXDIV; |
134 |
– |
} |
360 |
|
/* allocate scanlines */ |
361 |
|
for (i = 0; i <= psample; i++) { |
362 |
< |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
362 |
> |
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR)); |
363 |
|
if (scanbar[i] == NULL) |
364 |
|
goto memerr; |
365 |
|
} |
366 |
< |
/* open z file */ |
366 |
> |
hstep = (psample*140+49)/99; /* quincunx sampling */ |
367 |
> |
ystep = (psample*99+70)/140; |
368 |
> |
if (hstep > 2) { |
369 |
> |
i = hres/hstep + 2; |
370 |
> |
if ((sampdens = malloc(i)) == NULL) |
371 |
> |
goto memerr; |
372 |
> |
while (i--) |
373 |
> |
sampdens[i] = hstep; |
374 |
> |
} else |
375 |
> |
sampdens = NULL; |
376 |
> |
/* open z-file */ |
377 |
|
if (zfile != NULL) { |
378 |
|
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
379 |
< |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
379 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
380 |
|
error(SYSTEM, errmsg); |
381 |
|
} |
382 |
+ |
#ifdef MSDOS |
383 |
+ |
setmode(zfd, O_BINARY); |
384 |
+ |
#endif |
385 |
|
for (i = 0; i <= psample; i++) { |
386 |
< |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
386 |
> |
zbar[i] = (float *)malloc(hres*sizeof(float)); |
387 |
|
if (zbar[i] == NULL) |
388 |
|
goto memerr; |
389 |
|
} |
393 |
|
zbar[i] = NULL; |
394 |
|
} |
395 |
|
/* write out boundaries */ |
396 |
< |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
396 |
> |
fprtresolu(hres, vres, stdout); |
397 |
|
/* recover file and compute first */ |
398 |
|
i = salvage(oldfile); |
399 |
+ |
if (i >= vres) |
400 |
+ |
goto alldone; |
401 |
|
if (zfd != -1 && i > 0 && |
402 |
< |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
403 |
< |
error(SYSTEM, "z file seek error in render"); |
404 |
< |
pctdone = 100.0*i/vresolu; |
402 |
> |
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
403 |
> |
error(SYSTEM, "z-file seek error in render"); |
404 |
> |
pctdone = 100.0*i/vres; |
405 |
|
if (ralrm > 0) /* report init stats */ |
406 |
|
report(); |
407 |
< |
ypos = vresolu-1 - i; |
408 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
409 |
< |
ystep = psample; |
407 |
> |
#ifndef BSD |
408 |
> |
else |
409 |
> |
#endif |
410 |
> |
signal(SIGCONT, report); |
411 |
> |
ypos = vres-1 - i; /* initialize sampling */ |
412 |
> |
if (directvis) |
413 |
> |
init_drawsources(psample); |
414 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
415 |
|
/* compute scanlines */ |
416 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
417 |
|
/* bottom adjust? */ |
426 |
|
zbar[ystep] = zbar[0]; |
427 |
|
zbar[0] = zptr; |
428 |
|
/* fill base line */ |
429 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
429 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, |
430 |
> |
hres, ypos, hstep); |
431 |
|
/* fill bar */ |
432 |
< |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
432 |
> |
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
433 |
> |
if (directvis) /* add bitty sources */ |
434 |
> |
drawsources(scanbar, zbar, 0, hres, ypos, ystep); |
435 |
|
/* write it out */ |
436 |
+ |
#ifndef BSD |
437 |
+ |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
438 |
+ |
#endif |
439 |
|
for (i = ystep; i > 0; i--) { |
440 |
|
if (zfd != -1 && write(zfd, (char *)zbar[i], |
441 |
< |
hresolu*sizeof(float)) |
442 |
< |
< hresolu*sizeof(float)) |
441 |
> |
hres*sizeof(float)) |
442 |
> |
< hres*sizeof(float)) |
443 |
|
goto writerr; |
444 |
< |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
444 |
> |
if (fwritescan(scanbar[i], hres, stdout) < 0) |
445 |
|
goto writerr; |
446 |
|
} |
447 |
|
if (fflush(stdout) == EOF) |
448 |
|
goto writerr; |
449 |
|
/* record progress */ |
450 |
< |
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
451 |
< |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
450 |
> |
pctdone = 100.0*(vres-1-ypos)/vres; |
451 |
> |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
452 |
|
report(); |
453 |
+ |
#ifndef BSD |
454 |
+ |
else |
455 |
+ |
signal(SIGCONT, report); |
456 |
+ |
#endif |
457 |
|
} |
458 |
|
/* clean up */ |
459 |
+ |
signal(SIGCONT, SIG_IGN); |
460 |
+ |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) |
461 |
+ |
< hres*sizeof(float)) |
462 |
+ |
goto writerr; |
463 |
+ |
fwritescan(scanbar[0], hres, stdout); |
464 |
+ |
if (fflush(stdout) == EOF) |
465 |
+ |
goto writerr; |
466 |
+ |
alldone: |
467 |
|
if (zfd != -1) { |
205 |
– |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
206 |
– |
< hresolu*sizeof(float)) |
207 |
– |
goto writerr; |
468 |
|
if (close(zfd) == -1) |
469 |
|
goto writerr; |
470 |
|
for (i = 0; i <= psample; i++) |
471 |
|
free((char *)zbar[i]); |
472 |
|
} |
213 |
– |
fwritescan(scanbar[0], hresolu, stdout); |
214 |
– |
if (fflush(stdout) == EOF) |
215 |
– |
goto writerr; |
473 |
|
for (i = 0; i <= psample; i++) |
474 |
|
free((char *)scanbar[i]); |
475 |
+ |
if (sampdens != NULL) |
476 |
+ |
free(sampdens); |
477 |
|
pctdone = 100.0; |
478 |
+ |
if (ralrm > 0) |
479 |
+ |
report(); |
480 |
+ |
signal(SIGCONT, SIG_DFL); |
481 |
|
return; |
482 |
|
writerr: |
483 |
|
error(SYSTEM, "write error in render"); |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
< |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
490 |
< |
register COLOR *scanline; |
491 |
< |
register float *zline; |
489 |
> |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
490 |
> |
register COLOR *scanline; |
491 |
> |
register float *zline; |
492 |
> |
register char *sd; |
493 |
|
int xres, y, xstep; |
494 |
|
{ |
495 |
< |
int b = xstep; |
496 |
< |
double z; |
495 |
> |
static int nc = 0; /* number of calls */ |
496 |
> |
int bl = xstep, b = xstep; |
497 |
> |
double z; |
498 |
|
register int i; |
499 |
< |
|
499 |
> |
|
500 |
|
z = pixvalue(scanline[0], 0, y); |
501 |
|
if (zline) zline[0] = z; |
502 |
< |
|
503 |
< |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
502 |
> |
/* zig-zag start for quincunx pattern */ |
503 |
> |
for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) { |
504 |
|
if (i >= xres) { |
505 |
|
xstep += xres-1-i; |
506 |
|
i = xres-1; |
507 |
|
} |
508 |
|
z = pixvalue(scanline[i], i, y); |
509 |
|
if (zline) zline[i] = z; |
510 |
< |
|
511 |
< |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
512 |
< |
i-xstep, y, xstep, 0, b/2); |
510 |
> |
if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1]; |
511 |
> |
if (i <= xstep) |
512 |
> |
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
513 |
> |
else |
514 |
> |
b = fillsample(scanline+i-xstep, |
515 |
> |
zline ? zline+i-xstep : (float *)NULL, |
516 |
> |
i-xstep, y, xstep, 0, b/2); |
517 |
> |
if (sd) *sd++ = nc & 1 ? bl : b; |
518 |
> |
bl = b; |
519 |
|
} |
520 |
+ |
if (sd && nc & 1) *sd = bl; |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
|
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
525 |
< |
register COLOR *scanbar[]; |
526 |
< |
register float *zbar[]; |
525 |
> |
register COLOR *scanbar[]; |
526 |
> |
register float *zbar[]; |
527 |
|
int xres, y, ysize; |
528 |
|
{ |
529 |
|
COLOR vline[MAXDIV+1]; |
530 |
|
float zline[MAXDIV+1]; |
531 |
|
int b = ysize; |
532 |
|
register int i, j; |
533 |
< |
|
533 |
> |
|
534 |
|
for (i = 0; i < xres; i++) { |
264 |
– |
|
535 |
|
copycolor(vline[0], scanbar[0][i]); |
536 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
537 |
|
if (zbar[0]) { |
538 |
|
zline[0] = zbar[0][i]; |
539 |
|
zline[ysize] = zbar[ysize][i]; |
540 |
|
} |
541 |
< |
|
272 |
< |
b = fillsample(vline, zbar[0] ? zline : NULL, |
541 |
> |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL, |
542 |
|
i, y, 0, ysize, b/2); |
543 |
< |
|
543 |
> |
|
544 |
|
for (j = 1; j < ysize; j++) |
545 |
|
copycolor(scanbar[j][i], vline[j]); |
546 |
|
if (zbar[0]) |
551 |
|
|
552 |
|
|
553 |
|
int |
554 |
< |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
555 |
< |
register COLOR *colline; |
556 |
< |
register float *zline; |
554 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
555 |
> |
register COLOR *colline; |
556 |
> |
register float *zline; |
557 |
|
int x, y; |
558 |
|
int xlen, ylen; |
559 |
|
int b; |
560 |
|
{ |
561 |
< |
extern double fabs(); |
562 |
< |
double ratio; |
294 |
< |
double z; |
561 |
> |
double ratio; |
562 |
> |
double z; |
563 |
|
COLOR ctmp; |
564 |
|
int ncut; |
565 |
|
register int len; |
566 |
< |
|
566 |
> |
|
567 |
|
if (xlen > 0) /* x or y length is zero */ |
568 |
|
len = xlen; |
569 |
|
else |
570 |
|
len = ylen; |
571 |
< |
|
571 |
> |
|
572 |
|
if (len <= 1) /* limit recursion */ |
573 |
|
return(0); |
574 |
< |
|
575 |
< |
if (b > 0 |
576 |
< |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
574 |
> |
|
575 |
> |
if (b > 0 || |
576 |
> |
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
577 |
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
578 |
< |
|
578 |
> |
|
579 |
|
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
580 |
|
if (zline) zline[len>>1] = z; |
581 |
|
ncut = 1; |
314 |
– |
|
582 |
|
} else { /* interpolate */ |
316 |
– |
|
583 |
|
copycolor(colline[len>>1], colline[len]); |
584 |
|
ratio = (double)(len>>1) / len; |
585 |
|
scalecolor(colline[len>>1], ratio); |
593 |
|
} |
594 |
|
/* recurse */ |
595 |
|
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
596 |
< |
|
597 |
< |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
596 |
> |
|
597 |
> |
ncut += fillsample(colline+(len>>1), |
598 |
> |
zline ? zline+(len>>1) : (float *)NULL, |
599 |
|
x+(xlen>>1), y+(ylen>>1), |
600 |
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
601 |
|
|
608 |
|
COLOR col; /* returned color */ |
609 |
|
int x, y; /* pixel position */ |
610 |
|
{ |
611 |
< |
static RAY thisray; |
612 |
< |
|
613 |
< |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
614 |
< |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
611 |
> |
RAY thisray; |
612 |
> |
FVECT lorg, ldir; |
613 |
> |
double hpos, vpos, lmax, d; |
614 |
> |
/* compute view ray */ |
615 |
> |
hpos = (x+pixjitter())/hres; |
616 |
> |
vpos = (y+pixjitter())/vres; |
617 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
618 |
> |
&ourview, hpos, vpos)) < -FTINY) { |
619 |
|
setcolor(col, 0.0, 0.0, 0.0); |
620 |
|
return(0.0); |
621 |
|
} |
622 |
|
|
623 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
623 |
> |
samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ |
624 |
|
|
625 |
< |
samplendx = 3*y + x; /* set pixel index */ |
625 |
> |
/* optional motion blur */ |
626 |
> |
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, |
627 |
> |
&lastview, hpos, vpos)) >= -FTINY) { |
628 |
> |
register int i; |
629 |
> |
register double d = mblur*(.5-urand(281+samplendx)); |
630 |
|
|
631 |
+ |
thisray.rmax = (1.-d)*thisray.rmax + d*lmax; |
632 |
+ |
for (i = 3; i--; ) { |
633 |
+ |
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; |
634 |
+ |
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; |
635 |
+ |
} |
636 |
+ |
if (normalize(thisray.rdir) == 0.0) |
637 |
+ |
return(0.0); |
638 |
+ |
} |
639 |
+ |
|
640 |
+ |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
641 |
+ |
|
642 |
|
rayvalue(&thisray); /* trace ray */ |
643 |
|
|
644 |
|
copycolor(col, thisray.rcol); /* return color */ |
645 |
< |
|
645 |
> |
|
646 |
|
return(thisray.rt); /* return distance */ |
647 |
|
} |
648 |
|
|
656 |
|
int x, y; |
657 |
|
|
658 |
|
if (oldfile == NULL) |
659 |
< |
return(0); |
660 |
< |
|
659 |
> |
goto gotzip; |
660 |
> |
|
661 |
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
662 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
663 |
|
error(WARNING, errmsg); |
664 |
< |
return(0); |
664 |
> |
goto gotzip; |
665 |
|
} |
666 |
+ |
#ifdef MSDOS |
667 |
+ |
setmode(fileno(fp), O_BINARY); |
668 |
+ |
#endif |
669 |
|
/* discard header */ |
670 |
< |
getheader(fp, NULL); |
670 |
> |
getheader(fp, NULL, NULL); |
671 |
|
/* get picture size */ |
672 |
< |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
673 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
672 |
> |
if (!fscnresolu(&x, &y, fp)) { |
673 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
674 |
> |
oldfile); |
675 |
|
error(WARNING, errmsg); |
676 |
|
fclose(fp); |
677 |
< |
return(0); |
677 |
> |
goto gotzip; |
678 |
|
} |
679 |
|
|
680 |
< |
if (x != hresolu || y != vresolu) { |
680 |
> |
if (x != hres || y != vres) { |
681 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
682 |
|
oldfile); |
683 |
|
error(USER, errmsg); |
684 |
|
} |
685 |
|
|
686 |
< |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
686 |
> |
scanline = (COLR *)malloc(hres*sizeof(COLR)); |
687 |
|
if (scanline == NULL) |
688 |
|
error(SYSTEM, "out of memory in salvage"); |
689 |
< |
for (y = 0; y < vresolu; y++) { |
690 |
< |
if (freadcolrs(scanline, hresolu, fp) < 0) |
689 |
> |
for (y = 0; y < vres; y++) { |
690 |
> |
if (freadcolrs(scanline, hres, fp) < 0) |
691 |
|
break; |
692 |
< |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
692 |
> |
if (fwritecolrs(scanline, hres, stdout) < 0) |
693 |
|
goto writerr; |
694 |
|
} |
695 |
|
if (fflush(stdout) == EOF) |
698 |
|
fclose(fp); |
699 |
|
unlink(oldfile); |
700 |
|
return(y); |
701 |
+ |
gotzip: |
702 |
+ |
if (fflush(stdout) == EOF) |
703 |
+ |
error(SYSTEM, "error writing picture header"); |
704 |
+ |
return(0); |
705 |
|
writerr: |
706 |
< |
error(SYSTEM, "write error in salvage"); |
706 |
> |
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); |
707 |
> |
error(SYSTEM, errmsg); |
708 |
> |
} |
709 |
> |
|
710 |
> |
|
711 |
> |
int |
712 |
> |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
713 |
> |
register int x, y; |
714 |
> |
int xres, yres; |
715 |
> |
{ |
716 |
> |
x -= y; |
717 |
> |
while (x < 0) |
718 |
> |
x += xres; |
719 |
> |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
720 |
|
} |