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