1 |
– |
/* Copyright (c) 1991 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 |
|
* Compute pixels for glare calculation |
6 |
|
*/ |
7 |
|
|
8 |
|
#include "glare.h" |
9 |
< |
#include <sys/param.h> |
10 |
< |
/* 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) |
9 |
> |
/* maximum rtrace buffer size */ |
10 |
> |
#define MAXPIX (4096/(6*sizeof(float))) |
11 |
|
|
12 |
< |
#ifndef BSD |
20 |
< |
#define vfork fork |
21 |
< |
#endif |
22 |
< |
|
23 |
< |
#define MAXSBUF 524268 /* maximum total size of scanline buffer */ |
12 |
> |
#define MAXSBUF 786432 /* maximum total size of scanline buffer */ |
13 |
|
#define HSIZE 317 /* size of scanline hash table */ |
14 |
|
#define NRETIRE 16 /* number of scanlines to retire at once */ |
15 |
|
|
16 |
< |
int rt_pid = -1; /* process id for rtrace */ |
28 |
< |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
16 |
> |
int rt_pd[3] = {-1,-1,-1}; /* process id & descriptors for rtrace */ |
17 |
|
|
18 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
19 |
|
double exposure; /* picture exposure */ |
32 |
|
#define scandata(sl) ((COLR *)((sl)+1)) |
33 |
|
#define shash(y) ((y)%HSIZE) |
34 |
|
|
35 |
+ |
static int maxpix; /* maximum number of pixels to buffer */ |
36 |
+ |
|
37 |
|
static SCAN *freelist; /* scanline free list */ |
38 |
|
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
39 |
|
|
40 |
+ |
static long scanbufsiz; /* size of allocated scanline buffer */ |
41 |
+ |
|
42 |
|
static long ncall = 0L; /* number of calls to getpictscan */ |
43 |
|
static long nread = 0L; /* number of scanlines read */ |
44 |
|
static long nrecl = 0L; /* number of scanlines reclaimed */ |
152 |
|
FVECT vd; |
153 |
|
{ |
154 |
|
FVECT pp; |
155 |
< |
double vpx, vpy, vpz; |
155 |
> |
FVECT ip; |
156 |
|
COLOR res; |
157 |
|
|
158 |
|
if (pictfp == NULL) |
160 |
|
pp[0] = pictview.vp[0] + vd[0]; |
161 |
|
pp[1] = pictview.vp[1] + vd[1]; |
162 |
|
pp[2] = pictview.vp[2] + vd[2]; |
163 |
< |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
164 |
< |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
163 |
> |
viewloc(ip, &pictview, pp); |
164 |
> |
if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. || |
165 |
> |
ip[1] < 0. || ip[1] >= 1.) |
166 |
|
return(-1.0); |
167 |
< |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
167 |
> |
colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); |
168 |
|
return(luminance(res)/exposure); |
169 |
|
} |
170 |
|
|
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 |
> |
bzero(pb+6*np, 6*sizeof(float)); |
264 |
> |
if (process(rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1), |
265 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { |
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 |
|
|
273 |
+ |
int |
274 |
|
getexpos(s) /* get exposure from header line */ |
275 |
|
char *s; |
276 |
|
{ |
282 |
|
formatval(fmt, s); |
283 |
|
wrongformat = strcmp(fmt, COLRFMT); |
284 |
|
} |
285 |
+ |
return(0); |
286 |
|
} |
287 |
|
|
288 |
|
|
290 |
|
char *fn; |
291 |
|
{ |
292 |
|
if ((pictfp = fopen(fn, "r")) == NULL) { |
293 |
< |
fprintf("%s: cannot open\n", fn); |
293 |
> |
fprintf(stderr, "%s: cannot open\n", fn); |
294 |
|
exit(1); |
295 |
|
} |
296 |
|
exposure = 1.0; |
297 |
|
getheader(pictfp, getexpos, NULL); |
298 |
< |
if (wrongformat || |
299 |
< |
fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
312 |
< |
fprintf("%s: bad picture format\n", fn); |
298 |
> |
if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { |
299 |
> |
fprintf(stderr, "%s: incompatible picture format\n", fn); |
300 |
|
exit(1); |
301 |
|
} |
302 |
|
initscans(); |
316 |
|
fork_rtrace(av) /* open pipe and start rtrace */ |
317 |
|
char *av[]; |
318 |
|
{ |
319 |
< |
int p0[2], p1[2]; |
319 |
> |
int rval; |
320 |
|
|
321 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
321 |
> |
rval = open_process(rt_pd, av); |
322 |
> |
if (rval < 0) { |
323 |
|
perror(progname); |
324 |
|
exit(1); |
325 |
|
} |
326 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
327 |
< |
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] != 0) { /* 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); |
326 |
> |
if (rval == 0) { |
327 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
328 |
|
exit(1); |
329 |
|
} |
330 |
< |
close(p0[0]); |
331 |
< |
close(p1[1]); |
332 |
< |
fd_tort = p0[1]; |
333 |
< |
fd_fromrt = p1[0]; |
330 |
> |
maxpix = rval/(6*sizeof(float)); |
331 |
> |
if (maxpix > MAXPIX) |
332 |
> |
maxpix = MAXPIX; |
333 |
> |
maxpix--; |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
|
done_rtrace() /* wait for rtrace to finish */ |
338 |
|
{ |
339 |
< |
int pid, status; |
339 |
> |
int status; |
340 |
|
|
341 |
< |
if (rt_pid == -1) |
342 |
< |
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) { |
341 |
> |
status = close_process(rt_pd); |
342 |
> |
if (status > 0) { |
343 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
344 |
|
progname, status); |
345 |
|
exit(1); |
346 |
|
} |
347 |
< |
rt_pid = -1; |
347 |
> |
rt_pd[0] = -1; |
348 |
|
} |
349 |
|
|
350 |
|
|
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); |
415 |
– |
} |
416 |
– |
|
417 |
– |
|
351 |
|
SCAN * |
352 |
|
scanretire() /* retire old scanlines to free list */ |
353 |
|
{ |
398 |
|
register SCAN *ptr; |
399 |
|
register int i; |
400 |
|
/* initialize positions */ |
401 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
401 |
> |
scanpos = (long *)bmalloc(pysiz*sizeof(long)); |
402 |
|
if (scanpos == NULL) |
403 |
|
memerr("scanline positions"); |
404 |
|
for (i = pysiz-1; i >= 0; i--) |
410 |
|
/* allocate scanline buffers */ |
411 |
|
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
412 |
|
#ifdef ALIGN |
413 |
< |
scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1); |
413 |
> |
scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1); |
414 |
|
#endif |
415 |
|
i = MAXSBUF / scansize; /* compute number to allocate */ |
416 |
|
if (i > HSIZE) |
417 |
|
i = HSIZE; |
418 |
< |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
418 |
> |
scanbufsiz = i*scansize; |
419 |
> |
scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ |
420 |
|
if (scan_buf == NULL) |
421 |
|
memerr("scanline buffers"); |
422 |
|
ptr = (SCAN *)scan_buf; |
433 |
|
|
434 |
|
donescans() /* free up scanlines */ |
435 |
|
{ |
436 |
< |
free(scan_buf); |
437 |
< |
free((char *)scanpos); |
436 |
> |
bfree(scan_buf, scanbufsiz); |
437 |
> |
bfree((char *)scanpos, pysiz*sizeof(long)); |
438 |
|
} |