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 "copyright.h" |
9 |
+ |
|
10 |
+ |
#include <string.h> |
11 |
+ |
|
12 |
+ |
#include "rtprocess.h" /* Windows: must come first because of conflicts */ |
13 |
|
#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) |
14 |
|
|
15 |
< |
#ifndef BSD |
16 |
< |
#define vfork fork |
21 |
< |
#endif |
15 |
> |
/* maximum rtrace buffer size */ |
16 |
> |
#define MAXPIX (4096/(6*sizeof(float))) |
17 |
|
|
18 |
< |
#define MAXSBUF 524268 /* maximum total size of scanline buffer */ |
18 |
> |
#define MAXSBUF 786432 /* maximum total size of scanline buffer */ |
19 |
|
#define HSIZE 317 /* size of scanline hash table */ |
20 |
|
#define NRETIRE 16 /* number of scanlines to retire at once */ |
21 |
|
|
22 |
< |
int rt_pid = -1; /* process id for rtrace */ |
28 |
< |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
22 |
> |
static SUBPROC rt_pd = SP_INACTIVE; /* process id & descriptors for rtrace */ |
23 |
|
|
24 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
25 |
|
double exposure; /* picture exposure */ |
38 |
|
#define scandata(sl) ((COLR *)((sl)+1)) |
39 |
|
#define shash(y) ((y)%HSIZE) |
40 |
|
|
41 |
+ |
static int maxpix; /* maximum number of pixels to buffer */ |
42 |
+ |
|
43 |
|
static SCAN *freelist; /* scanline free list */ |
44 |
|
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
45 |
|
|
46 |
+ |
static long scanbufsiz; /* size of allocated scanline buffer */ |
47 |
+ |
|
48 |
|
static long ncall = 0L; /* number of calls to getpictscan */ |
49 |
|
static long nread = 0L; /* number of scanlines read */ |
50 |
|
static long nrecl = 0L; /* number of scanlines reclaimed */ |
158 |
|
FVECT vd; |
159 |
|
{ |
160 |
|
FVECT pp; |
161 |
< |
double vpx, vpy, vpz; |
161 |
> |
FVECT ip; |
162 |
|
COLOR res; |
163 |
|
|
164 |
|
if (pictfp == NULL) |
166 |
|
pp[0] = pictview.vp[0] + vd[0]; |
167 |
|
pp[1] = pictview.vp[1] + vd[1]; |
168 |
|
pp[2] = pictview.vp[2] + vd[2]; |
169 |
< |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
170 |
< |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
169 |
> |
viewloc(ip, &pictview, pp); |
170 |
> |
if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. || |
171 |
> |
ip[1] < 0. || ip[1] >= 1.) |
172 |
|
return(-1.0); |
173 |
< |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
173 |
> |
colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); |
174 |
|
return(luminance(res)/exposure); |
175 |
|
} |
176 |
|
|
180 |
|
int vh, vv; |
181 |
|
{ |
182 |
|
FVECT dir; |
183 |
< |
float rt_buf[6]; |
183 |
> |
float rt_buf[12]; |
184 |
|
double res; |
185 |
|
|
186 |
|
if (compdir(dir, vh, vv) < 0) |
188 |
|
npixinvw++; |
189 |
|
if ((res = pict_val(dir)) >= 0.0) |
190 |
|
return(res); |
191 |
< |
if (rt_pid == -1) { |
191 |
> |
if (rt_pd.r == -1) { |
192 |
|
npixmiss++; |
193 |
|
return(-1.0); |
194 |
|
} |
227 |
|
npixinvw++; |
228 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
229 |
|
continue; |
230 |
< |
if (rt_pid == -1) { /* missing information */ |
230 |
> |
if (rt_pd.r == -1) { /* missing information */ |
231 |
|
npixmiss++; |
232 |
|
continue; |
233 |
|
} |
234 |
|
/* send to rtrace */ |
235 |
< |
if (n >= MAXPIX) { /* flush */ |
235 |
> |
if (n >= maxpix) { /* flush */ |
236 |
|
rt_compute(rt_buf, n); |
237 |
|
while (n-- > 0) |
238 |
|
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
261 |
|
float *pb; |
262 |
|
int np; |
263 |
|
{ |
265 |
– |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
266 |
– |
|
264 |
|
#ifdef DEBUG |
265 |
|
if (verbose && np > 1) |
266 |
|
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
267 |
|
progname, np); |
268 |
|
#endif |
269 |
< |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
270 |
< |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
271 |
< |
fprintf(stderr, "%s: error writing to rtrace process\n", |
269 |
> |
memset(pb+6*np, '\0', 6*sizeof(float)); |
270 |
> |
if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1), |
271 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { |
272 |
> |
fprintf(stderr, "%s: rtrace communication error\n", |
273 |
|
progname); |
274 |
|
exit(1); |
275 |
|
} |
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 |
– |
} |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
+ |
int |
280 |
|
getexpos(s) /* get exposure from header line */ |
281 |
|
char *s; |
282 |
|
{ |
288 |
|
formatval(fmt, s); |
289 |
|
wrongformat = strcmp(fmt, COLRFMT); |
290 |
|
} |
291 |
+ |
return(0); |
292 |
|
} |
293 |
|
|
294 |
|
|
296 |
|
char *fn; |
297 |
|
{ |
298 |
|
if ((pictfp = fopen(fn, "r")) == NULL) { |
299 |
< |
fprintf("%s: cannot open\n", fn); |
299 |
> |
fprintf(stderr, "%s: cannot open\n", fn); |
300 |
|
exit(1); |
301 |
|
} |
302 |
|
exposure = 1.0; |
303 |
|
getheader(pictfp, getexpos, NULL); |
304 |
< |
if (wrongformat || |
305 |
< |
fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
312 |
< |
fprintf("%s: bad picture format\n", fn); |
304 |
> |
if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { |
305 |
> |
fprintf(stderr, "%s: incompatible picture format\n", fn); |
306 |
|
exit(1); |
307 |
|
} |
308 |
|
initscans(); |
322 |
|
fork_rtrace(av) /* open pipe and start rtrace */ |
323 |
|
char *av[]; |
324 |
|
{ |
325 |
< |
int p0[2], p1[2]; |
325 |
> |
int rval; |
326 |
|
|
327 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
327 |
> |
rval = open_process(&rt_pd, av); |
328 |
> |
if (rval < 0) { |
329 |
|
perror(progname); |
330 |
|
exit(1); |
331 |
|
} |
332 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
333 |
< |
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); |
332 |
> |
if (rval == 0) { |
333 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
334 |
|
exit(1); |
335 |
|
} |
336 |
< |
close(p0[0]); |
337 |
< |
close(p1[1]); |
338 |
< |
fd_tort = p0[1]; |
339 |
< |
fd_fromrt = p1[0]; |
336 |
> |
maxpix = rval/(6*sizeof(float)); |
337 |
> |
if (maxpix > MAXPIX) |
338 |
> |
maxpix = MAXPIX; |
339 |
> |
maxpix--; |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
done_rtrace() /* wait for rtrace to finish */ |
344 |
|
{ |
345 |
< |
int pid, status; |
345 |
> |
int status; |
346 |
|
|
347 |
< |
if (rt_pid == -1) |
348 |
< |
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) { |
347 |
> |
status = close_process(&rt_pd); |
348 |
> |
if (status > 0) { |
349 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
350 |
|
progname, status); |
351 |
|
exit(1); |
352 |
|
} |
353 |
< |
rt_pid = -1; |
353 |
> |
rt_pd.r = -1; |
354 |
|
} |
355 |
|
|
356 |
|
|
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 |
– |
|
357 |
|
SCAN * |
358 |
|
scanretire() /* retire old scanlines to free list */ |
359 |
|
{ |
404 |
|
register SCAN *ptr; |
405 |
|
register int i; |
406 |
|
/* initialize positions */ |
407 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
407 |
> |
scanpos = (long *)bmalloc(pysiz*sizeof(long)); |
408 |
|
if (scanpos == NULL) |
409 |
|
memerr("scanline positions"); |
410 |
|
for (i = pysiz-1; i >= 0; i--) |
415 |
|
hashtab[i] = NULL; |
416 |
|
/* allocate scanline buffers */ |
417 |
|
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
418 |
< |
#ifdef ALIGN |
419 |
< |
scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1); |
418 |
> |
#ifdef ALIGNT |
419 |
> |
scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1); |
420 |
|
#endif |
421 |
|
i = MAXSBUF / scansize; /* compute number to allocate */ |
422 |
|
if (i > HSIZE) |
423 |
|
i = HSIZE; |
424 |
< |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
424 |
> |
scanbufsiz = i*scansize; |
425 |
> |
scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ |
426 |
|
if (scan_buf == NULL) |
427 |
|
memerr("scanline buffers"); |
428 |
|
ptr = (SCAN *)scan_buf; |
439 |
|
|
440 |
|
donescans() /* free up scanlines */ |
441 |
|
{ |
442 |
< |
free(scan_buf); |
443 |
< |
free((char *)scanpos); |
442 |
> |
bfree(scan_buf, scanbufsiz); |
443 |
> |
bfree((char *)scanpos, pysiz*sizeof(long)); |
444 |
|
} |