9 |
|
*/ |
10 |
|
|
11 |
|
#include "glare.h" |
12 |
< |
#include <sys/param.h> |
13 |
< |
/* compute rtrace buffer size */ |
14 |
< |
#ifndef PIPE_BUF |
15 |
< |
#define PIPE_BUF 512 /* hyperconservative */ |
16 |
< |
#endif |
17 |
< |
#define MAXPIX (PIPE_BUF/(6*sizeof(float)) - 1) |
12 |
> |
#include "resolu.h" |
13 |
> |
/* maximum rtrace buffer size */ |
14 |
> |
#define MAXPIX (4096/(6*sizeof(float))) |
15 |
|
|
16 |
< |
#ifndef BSD |
20 |
< |
#define vfork fork |
21 |
< |
#endif |
22 |
< |
|
23 |
< |
#define MAXSBUF 524268 /* maximum total size of scanline buffer */ |
16 |
> |
#define MAXSBUF 786432 /* maximum total size of scanline buffer */ |
17 |
|
#define HSIZE 317 /* size of scanline hash table */ |
18 |
|
#define NRETIRE 16 /* number of scanlines to retire at once */ |
19 |
|
|
20 |
< |
int rt_pid = -1; /* process id for rtrace */ |
28 |
< |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
20 |
> |
int rt_pd[3] = {-1,-1,-1}; /* process id & descriptors for rtrace */ |
21 |
|
|
22 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
23 |
|
double exposure; /* picture exposure */ |
36 |
|
#define scandata(sl) ((COLR *)((sl)+1)) |
37 |
|
#define shash(y) ((y)%HSIZE) |
38 |
|
|
39 |
+ |
static int maxpix; /* maximum number of pixels to buffer */ |
40 |
+ |
|
41 |
|
static SCAN *freelist; /* scanline free list */ |
42 |
|
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
43 |
|
|
44 |
+ |
static long scanbufsiz; /* size of allocated scanline buffer */ |
45 |
+ |
|
46 |
|
static long ncall = 0L; /* number of calls to getpictscan */ |
47 |
|
static long nread = 0L; /* number of scanlines read */ |
48 |
+ |
static long nrecl = 0L; /* number of scanlines reclaimed */ |
49 |
|
|
50 |
+ |
static int wrongformat = 0; |
51 |
+ |
|
52 |
|
SCAN *scanretire(); |
53 |
|
|
54 |
|
extern long ftell(); |
74 |
|
if (sl->y == y) { /* reclaim */ |
75 |
|
sl->next = hashtab[hi]; |
76 |
|
hashtab[hi] = sl; |
77 |
< |
#ifdef DEBUG |
79 |
< |
if (verbose) |
80 |
< |
fprintf(stderr, |
81 |
< |
"%s: scanline %d reclaimed\n", |
82 |
< |
progname, y); |
83 |
< |
#endif |
77 |
> |
nrecl++; |
78 |
|
} |
79 |
|
return(sl); |
80 |
|
} |
138 |
|
{ |
139 |
|
static long lastcall = 0L; /* ncall at last report */ |
140 |
|
static long lastread = 0L; /* nread at last report */ |
141 |
+ |
static long lastrecl = 0L; /* nrecl at last report */ |
142 |
|
|
143 |
|
if (ncall == lastcall) |
144 |
|
return; |
145 |
< |
fprintf(stderr, "%s: %ld scanlines read in %ld calls\n", |
146 |
< |
progname, nread-lastread, ncall-lastcall); |
145 |
> |
fprintf(stderr, "%s: %ld scanlines read (%ld reclaimed) in %ld calls\n", |
146 |
> |
progname, nread-lastread, nrecl-lastrecl, ncall-lastcall); |
147 |
|
lastcall = ncall; |
148 |
|
lastread = nread; |
149 |
+ |
lastrecl = nrecl; |
150 |
|
} |
151 |
|
#endif |
152 |
|
|
156 |
|
FVECT vd; |
157 |
|
{ |
158 |
|
FVECT pp; |
159 |
< |
double vpx, vpy, vpz; |
159 |
> |
FVECT ip; |
160 |
|
COLOR res; |
161 |
|
|
162 |
|
if (pictfp == NULL) |
164 |
|
pp[0] = pictview.vp[0] + vd[0]; |
165 |
|
pp[1] = pictview.vp[1] + vd[1]; |
166 |
|
pp[2] = pictview.vp[2] + vd[2]; |
167 |
< |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
168 |
< |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
167 |
> |
viewloc(ip, &pictview, pp); |
168 |
> |
if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. || |
169 |
> |
ip[1] < 0. || ip[1] >= 1.) |
170 |
|
return(-1.0); |
171 |
< |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
171 |
> |
colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); |
172 |
|
return(luminance(res)/exposure); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
double |
177 |
< |
getviewpix(vh, vv, se) /* compute single view pixel */ |
177 |
> |
getviewpix(vh, vv) /* compute single view pixel */ |
178 |
|
int vh, vv; |
182 |
– |
SPANERR *se; |
179 |
|
{ |
180 |
|
FVECT dir; |
181 |
< |
float rt_buf[6]; |
181 |
> |
float rt_buf[12]; |
182 |
|
double res; |
183 |
|
|
184 |
< |
if (compdir(dir, vh, vv, se) < 0) |
184 |
> |
if (compdir(dir, vh, vv) < 0) |
185 |
|
return(-1.0); |
186 |
|
npixinvw++; |
187 |
|
if ((res = pict_val(dir)) >= 0.0) |
188 |
|
return(res); |
189 |
< |
if (rt_pid == -1) { |
189 |
> |
if (rt_pd[0] == -1) { |
190 |
|
npixmiss++; |
191 |
|
return(-1.0); |
192 |
|
} |
208 |
|
float rt_buf[6*MAXPIX]; /* rtrace send/receive buffer */ |
209 |
|
register int n; /* number of pixels in buffer */ |
210 |
|
short buf_vh[MAXPIX]; /* pixel positions */ |
215 |
– |
SPANERR sperr; |
211 |
|
FVECT dir; |
212 |
|
register int vh; |
213 |
|
|
216 |
|
fprintf(stderr, "%s: computing view span at %d...\n", |
217 |
|
progname, vv); |
218 |
|
#endif |
224 |
– |
setspanerr(&sperr, vv); |
219 |
|
n = 0; |
220 |
|
for (vh = -hsize; vh <= hsize; vh++) { |
221 |
< |
if (compdir(dir, vh, vv, &sperr) < 0) { /* not in view */ |
221 |
> |
if (compdir(dir, vh, vv) < 0) { /* not in view */ |
222 |
|
vb[vh+hsize] = -1.0; |
223 |
|
continue; |
224 |
|
} |
225 |
|
npixinvw++; |
226 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
227 |
|
continue; |
228 |
< |
if (rt_pid == -1) { /* missing information */ |
228 |
> |
if (rt_pd[0] == -1) { /* missing information */ |
229 |
|
npixmiss++; |
230 |
|
continue; |
231 |
|
} |
232 |
|
/* send to rtrace */ |
233 |
< |
if (n >= MAXPIX) { /* flush */ |
233 |
> |
if (n >= maxpix) { /* flush */ |
234 |
|
rt_compute(rt_buf, n); |
235 |
|
while (n-- > 0) |
236 |
|
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
259 |
|
float *pb; |
260 |
|
int np; |
261 |
|
{ |
268 |
– |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
269 |
– |
|
262 |
|
#ifdef DEBUG |
263 |
|
if (verbose && np > 1) |
264 |
|
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
265 |
|
progname, np); |
266 |
|
#endif |
267 |
< |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
268 |
< |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
269 |
< |
fprintf(stderr, "%s: error writing to rtrace process\n", |
267 |
> |
bzero(pb+6*np, 6*sizeof(float)); |
268 |
> |
if (process(rt_pd, pb, pb, 3*sizeof(float)*(np+1), |
269 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { |
270 |
> |
fprintf(stderr, "%s: rtrace communication error\n", |
271 |
|
progname); |
272 |
|
exit(1); |
273 |
|
} |
281 |
– |
if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np) |
282 |
– |
< 3*sizeof(float)*np) { |
283 |
– |
fprintf(stderr, "%s: error reading from rtrace process\n", |
284 |
– |
progname); |
285 |
– |
exit(1); |
286 |
– |
} |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
+ |
int |
278 |
|
getexpos(s) /* get exposure from header line */ |
279 |
|
char *s; |
280 |
|
{ |
281 |
+ |
char fmt[32]; |
282 |
+ |
|
283 |
|
if (isexpos(s)) |
284 |
|
exposure *= exposval(s); |
285 |
+ |
else if (isformat(s)) { |
286 |
+ |
formatval(fmt, s); |
287 |
+ |
wrongformat = strcmp(fmt, COLRFMT); |
288 |
+ |
} |
289 |
+ |
return(0); |
290 |
|
} |
291 |
|
|
292 |
|
|
294 |
|
char *fn; |
295 |
|
{ |
296 |
|
if ((pictfp = fopen(fn, "r")) == NULL) { |
297 |
< |
fprintf("%s: cannot open\n", fn); |
297 |
> |
fprintf(stderr, "%s: cannot open\n", fn); |
298 |
|
exit(1); |
299 |
|
} |
300 |
|
exposure = 1.0; |
301 |
< |
getheader(pictfp, getexpos); |
302 |
< |
if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
303 |
< |
fprintf("%s: bad picture resolution\n", fn); |
301 |
> |
getheader(pictfp, getexpos, NULL); |
302 |
> |
if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { |
303 |
> |
fprintf(stderr, "%s: incompatible picture format\n", fn); |
304 |
|
exit(1); |
305 |
|
} |
306 |
|
initscans(); |
320 |
|
fork_rtrace(av) /* open pipe and start rtrace */ |
321 |
|
char *av[]; |
322 |
|
{ |
323 |
< |
int p0[2], p1[2]; |
323 |
> |
int rval; |
324 |
|
|
325 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
325 |
> |
rval = open_process(rt_pd, av); |
326 |
> |
if (rval < 0) { |
327 |
|
perror(progname); |
328 |
|
exit(1); |
329 |
|
} |
330 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
331 |
< |
close(p0[1]); |
336 |
< |
close(p1[0]); |
337 |
< |
if (p0[0] != 0) { /* connect p0 to stdin */ |
338 |
< |
dup2(p0[0], 0); |
339 |
< |
close(p0[0]); |
340 |
< |
} |
341 |
< |
if (p1[1] != 0) { /* connect p1 to stdout */ |
342 |
< |
dup2(p1[1], 1); |
343 |
< |
close(p1[1]); |
344 |
< |
} |
345 |
< |
execvp(av[0], av); |
346 |
< |
perror(av[0]); |
347 |
< |
_exit(127); |
348 |
< |
} |
349 |
< |
if (rt_pid == -1) { |
350 |
< |
perror(progname); |
330 |
> |
if (rval == 0) { |
331 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
332 |
|
exit(1); |
333 |
|
} |
334 |
< |
close(p0[0]); |
335 |
< |
close(p1[1]); |
336 |
< |
fd_tort = p0[1]; |
337 |
< |
fd_fromrt = p1[0]; |
334 |
> |
maxpix = rval/(6*sizeof(float)); |
335 |
> |
if (maxpix > MAXPIX) |
336 |
> |
maxpix = MAXPIX; |
337 |
> |
maxpix--; |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
done_rtrace() /* wait for rtrace to finish */ |
342 |
|
{ |
343 |
< |
int pid, status; |
343 |
> |
int status; |
344 |
|
|
345 |
< |
if (rt_pid == -1) |
346 |
< |
return; |
366 |
< |
close(fd_tort); |
367 |
< |
close(fd_fromrt); |
368 |
< |
while ((pid = wait(&status)) != -1 && pid != rt_pid) |
369 |
< |
; |
370 |
< |
if (pid == rt_pid && status != 0) { |
345 |
> |
status = close_process(rt_pd); |
346 |
> |
if (status > 0) { |
347 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
348 |
|
progname, status); |
349 |
|
exit(1); |
350 |
|
} |
351 |
< |
rt_pid = -1; |
351 |
> |
rt_pd[0] = -1; |
352 |
|
} |
353 |
|
|
354 |
|
|
379 |
– |
int |
380 |
– |
readbuf(fd, bpos, siz) |
381 |
– |
int fd; |
382 |
– |
char *bpos; |
383 |
– |
int siz; |
384 |
– |
{ |
385 |
– |
register int cc, nrem = siz; |
386 |
– |
|
387 |
– |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
388 |
– |
bpos += cc; |
389 |
– |
nrem -= cc; |
390 |
– |
} |
391 |
– |
if (cc < 0) |
392 |
– |
return(cc); |
393 |
– |
return(siz-nrem); |
394 |
– |
} |
395 |
– |
|
396 |
– |
|
397 |
– |
int |
398 |
– |
writebuf(fd, bpos, siz) |
399 |
– |
char *bpos; |
400 |
– |
int siz; |
401 |
– |
{ |
402 |
– |
register int cc, nrem = siz; |
403 |
– |
|
404 |
– |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
405 |
– |
bpos += cc; |
406 |
– |
nrem -= cc; |
407 |
– |
} |
408 |
– |
if (cc < 0) |
409 |
– |
return(cc); |
410 |
– |
return(siz-nrem); |
411 |
– |
} |
412 |
– |
|
413 |
– |
|
355 |
|
SCAN * |
356 |
|
scanretire() /* retire old scanlines to free list */ |
357 |
|
{ |
402 |
|
register SCAN *ptr; |
403 |
|
register int i; |
404 |
|
/* initialize positions */ |
405 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
405 |
> |
scanpos = (long *)bmalloc(pysiz*sizeof(long)); |
406 |
|
if (scanpos == NULL) |
407 |
|
memerr("scanline positions"); |
408 |
|
for (i = pysiz-1; i >= 0; i--) |
414 |
|
/* allocate scanline buffers */ |
415 |
|
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
416 |
|
#ifdef ALIGN |
417 |
< |
scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1); |
417 |
> |
scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1); |
418 |
|
#endif |
419 |
|
i = MAXSBUF / scansize; /* compute number to allocate */ |
420 |
|
if (i > HSIZE) |
421 |
|
i = HSIZE; |
422 |
< |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
422 |
> |
scanbufsiz = i*scansize; |
423 |
> |
scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ |
424 |
|
if (scan_buf == NULL) |
425 |
|
memerr("scanline buffers"); |
426 |
|
ptr = (SCAN *)scan_buf; |
437 |
|
|
438 |
|
donescans() /* free up scanlines */ |
439 |
|
{ |
440 |
< |
free(scan_buf); |
441 |
< |
free((char *)scanpos); |
440 |
> |
bfree(scan_buf, scanbufsiz); |
441 |
> |
bfree((char *)scanpos, pysiz*sizeof(long)); |
442 |
|
} |