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 |
> |
/* maximum rtrace buffer size */ |
13 |
> |
#define MAXPIX (4096/(6*sizeof(float))) |
14 |
|
|
19 |
– |
#ifndef BSD |
20 |
– |
#define vfork fork |
21 |
– |
#endif |
22 |
– |
|
15 |
|
#define MAXSBUF 786432 /* maximum total size of scanline buffer */ |
16 |
|
#define HSIZE 317 /* size of scanline hash table */ |
17 |
|
#define NRETIRE 16 /* number of scanlines to retire at once */ |
18 |
|
|
19 |
< |
int rt_pid = -1; /* process id for rtrace */ |
28 |
< |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
19 |
> |
int rt_pd[3] = {-1,-1,-1}; /* process id & descriptors for rtrace */ |
20 |
|
|
21 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
22 |
|
double exposure; /* picture exposure */ |
35 |
|
#define scandata(sl) ((COLR *)((sl)+1)) |
36 |
|
#define shash(y) ((y)%HSIZE) |
37 |
|
|
38 |
+ |
static int maxpix; /* maximum number of pixels to buffer */ |
39 |
+ |
|
40 |
|
static SCAN *freelist; /* scanline free list */ |
41 |
|
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
42 |
|
|
174 |
|
int vh, vv; |
175 |
|
{ |
176 |
|
FVECT dir; |
177 |
< |
float rt_buf[6]; |
177 |
> |
float rt_buf[12]; |
178 |
|
double res; |
179 |
|
|
180 |
|
if (compdir(dir, vh, vv) < 0) |
182 |
|
npixinvw++; |
183 |
|
if ((res = pict_val(dir)) >= 0.0) |
184 |
|
return(res); |
185 |
< |
if (rt_pid == -1) { |
185 |
> |
if (rt_pd[0] == -1) { |
186 |
|
npixmiss++; |
187 |
|
return(-1.0); |
188 |
|
} |
221 |
|
npixinvw++; |
222 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
223 |
|
continue; |
224 |
< |
if (rt_pid == -1) { /* missing information */ |
224 |
> |
if (rt_pd[0] == -1) { /* missing information */ |
225 |
|
npixmiss++; |
226 |
|
continue; |
227 |
|
} |
228 |
|
/* send to rtrace */ |
229 |
< |
if (n >= MAXPIX) { /* flush */ |
229 |
> |
if (n >= maxpix) { /* flush */ |
230 |
|
rt_compute(rt_buf, n); |
231 |
|
while (n-- > 0) |
232 |
|
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
255 |
|
float *pb; |
256 |
|
int np; |
257 |
|
{ |
265 |
– |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
266 |
– |
|
258 |
|
#ifdef DEBUG |
259 |
|
if (verbose && np > 1) |
260 |
|
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
261 |
|
progname, np); |
262 |
|
#endif |
263 |
< |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
264 |
< |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
265 |
< |
fprintf(stderr, "%s: error writing to rtrace process\n", |
263 |
> |
pb[6*np+3] = 0.; pb[6*np+4] = 0.; pb[6*np+5] = 0.; |
264 |
> |
if (process(rt_pd, pb, pb, 3*sizeof(float)*np, |
265 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*np) { |
266 |
> |
fprintf(stderr, "%s: rtrace communication error\n", |
267 |
|
progname); |
268 |
|
exit(1); |
269 |
|
} |
278 |
– |
if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np) |
279 |
– |
< 3*sizeof(float)*np) { |
280 |
– |
fprintf(stderr, "%s: error reading from rtrace process\n", |
281 |
– |
progname); |
282 |
– |
exit(1); |
283 |
– |
} |
270 |
|
} |
271 |
|
|
272 |
|
|
315 |
|
fork_rtrace(av) /* open pipe and start rtrace */ |
316 |
|
char *av[]; |
317 |
|
{ |
318 |
< |
int p0[2], p1[2]; |
318 |
> |
int rval; |
319 |
|
|
320 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
320 |
> |
rval = open_process(rt_pd, av); |
321 |
> |
if (rval < 0) { |
322 |
|
perror(progname); |
323 |
|
exit(1); |
324 |
|
} |
325 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
326 |
< |
close(p0[1]); |
340 |
< |
close(p1[0]); |
341 |
< |
if (p0[0] != 0) { /* connect p0 to stdin */ |
342 |
< |
dup2(p0[0], 0); |
343 |
< |
close(p0[0]); |
344 |
< |
} |
345 |
< |
if (p1[1] != 1) { /* connect p1 to stdout */ |
346 |
< |
dup2(p1[1], 1); |
347 |
< |
close(p1[1]); |
348 |
< |
} |
349 |
< |
execvp(av[0], av); |
350 |
< |
perror(av[0]); |
351 |
< |
_exit(127); |
352 |
< |
} |
353 |
< |
if (rt_pid == -1) { |
354 |
< |
perror(progname); |
325 |
> |
if (rval == 0) { |
326 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
327 |
|
exit(1); |
328 |
|
} |
329 |
< |
close(p0[0]); |
330 |
< |
close(p1[1]); |
331 |
< |
fd_tort = p0[1]; |
332 |
< |
fd_fromrt = p1[0]; |
329 |
> |
maxpix = rval/(6*sizeof(float)); |
330 |
> |
if (maxpix > MAXPIX) |
331 |
> |
maxpix = MAXPIX; |
332 |
> |
maxpix--; |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
done_rtrace() /* wait for rtrace to finish */ |
337 |
|
{ |
338 |
< |
int pid, status; |
338 |
> |
int status; |
339 |
|
|
340 |
< |
if (rt_pid == -1) |
341 |
< |
return; |
370 |
< |
close(fd_tort); |
371 |
< |
close(fd_fromrt); |
372 |
< |
while ((pid = wait(&status)) != -1 && pid != rt_pid) |
373 |
< |
; |
374 |
< |
if (pid == rt_pid && status != 0) { |
340 |
> |
status = close_process(rt_pd); |
341 |
> |
if (status > 0) { |
342 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
343 |
|
progname, status); |
344 |
|
exit(1); |
345 |
|
} |
346 |
< |
rt_pid = -1; |
380 |
< |
} |
381 |
< |
|
382 |
< |
|
383 |
< |
int |
384 |
< |
readbuf(fd, bpos, siz) |
385 |
< |
int fd; |
386 |
< |
char *bpos; |
387 |
< |
int siz; |
388 |
< |
{ |
389 |
< |
register int cc, nrem = siz; |
390 |
< |
|
391 |
< |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
392 |
< |
bpos += cc; |
393 |
< |
nrem -= cc; |
394 |
< |
} |
395 |
< |
if (cc < 0) |
396 |
< |
return(cc); |
397 |
< |
return(siz-nrem); |
398 |
< |
} |
399 |
< |
|
400 |
< |
|
401 |
< |
int |
402 |
< |
writebuf(fd, bpos, siz) |
403 |
< |
char *bpos; |
404 |
< |
int siz; |
405 |
< |
{ |
406 |
< |
register int cc, nrem = siz; |
407 |
< |
|
408 |
< |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
409 |
< |
bpos += cc; |
410 |
< |
nrem -= cc; |
411 |
< |
} |
412 |
< |
if (cc < 0) |
413 |
< |
return(cc); |
414 |
< |
return(siz-nrem); |
346 |
> |
rt_pd[0] = -1; |
347 |
|
} |
348 |
|
|
349 |
|
|