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 <stdlib.h> |
11 |
+ |
#include <string.h> |
12 |
+ |
|
13 |
+ |
#include "rtprocess.h" /* Windows: must come first because of conflicts */ |
14 |
|
#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) |
15 |
|
|
16 |
< |
#ifndef BSD |
17 |
< |
#define vfork fork |
21 |
< |
#endif |
16 |
> |
/* maximum rtrace buffer size */ |
17 |
> |
#define MAXPIX (4096/(6*sizeof(float))) |
18 |
|
|
19 |
< |
#define MAXSBUF 1023980 /* maximum total size of scanline buffer */ |
19 |
> |
#define MAXSBUF 786432 /* maximum total size of scanline buffer */ |
20 |
|
#define HSIZE 317 /* size of scanline hash table */ |
21 |
|
#define NRETIRE 16 /* number of scanlines to retire at once */ |
22 |
|
|
23 |
< |
int rt_pid = -1; /* process id for rtrace */ |
28 |
< |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
23 |
> |
static SUBPROC rt_pd = SP_INACTIVE; /* process id & descriptors for rtrace */ |
24 |
|
|
25 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
26 |
|
double exposure; /* picture exposure */ |
39 |
|
#define scandata(sl) ((COLR *)((sl)+1)) |
40 |
|
#define shash(y) ((y)%HSIZE) |
41 |
|
|
42 |
+ |
static int maxpix; /* maximum number of pixels to buffer */ |
43 |
+ |
|
44 |
|
static SCAN *freelist; /* scanline free list */ |
45 |
|
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
46 |
|
|
47 |
+ |
static long scanbufsiz; /* size of allocated scanline buffer */ |
48 |
+ |
|
49 |
|
static long ncall = 0L; /* number of calls to getpictscan */ |
50 |
|
static long nread = 0L; /* number of scanlines read */ |
51 |
+ |
static long nrecl = 0L; /* number of scanlines reclaimed */ |
52 |
|
|
53 |
+ |
static int wrongformat = 0; |
54 |
+ |
|
55 |
|
SCAN *scanretire(); |
56 |
|
|
57 |
|
extern long ftell(); |
77 |
|
if (sl->y == y) { /* reclaim */ |
78 |
|
sl->next = hashtab[hi]; |
79 |
|
hashtab[hi] = sl; |
80 |
+ |
nrecl++; |
81 |
|
} |
82 |
|
return(sl); |
83 |
|
} |
141 |
|
{ |
142 |
|
static long lastcall = 0L; /* ncall at last report */ |
143 |
|
static long lastread = 0L; /* nread at last report */ |
144 |
+ |
static long lastrecl = 0L; /* nrecl at last report */ |
145 |
|
|
146 |
|
if (ncall == lastcall) |
147 |
|
return; |
148 |
< |
fprintf(stderr, "%s: %ld scanlines read in %ld calls\n", |
149 |
< |
progname, nread-lastread, ncall-lastcall); |
148 |
> |
fprintf(stderr, "%s: %ld scanlines read (%ld reclaimed) in %ld calls\n", |
149 |
> |
progname, nread-lastread, nrecl-lastrecl, ncall-lastcall); |
150 |
|
lastcall = ncall; |
151 |
|
lastread = nread; |
152 |
+ |
lastrecl = nrecl; |
153 |
|
} |
154 |
|
#endif |
155 |
|
|
159 |
|
FVECT vd; |
160 |
|
{ |
161 |
|
FVECT pp; |
162 |
< |
double vpx, vpy, vpz; |
162 |
> |
FVECT ip; |
163 |
|
COLOR res; |
164 |
|
|
165 |
|
if (pictfp == NULL) |
167 |
|
pp[0] = pictview.vp[0] + vd[0]; |
168 |
|
pp[1] = pictview.vp[1] + vd[1]; |
169 |
|
pp[2] = pictview.vp[2] + vd[2]; |
170 |
< |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
171 |
< |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
170 |
> |
viewloc(ip, &pictview, pp); |
171 |
> |
if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. || |
172 |
> |
ip[1] < 0. || ip[1] >= 1.) |
173 |
|
return(-1.0); |
174 |
< |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
174 |
> |
colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); |
175 |
|
return(luminance(res)/exposure); |
176 |
|
} |
177 |
|
|
181 |
|
int vh, vv; |
182 |
|
{ |
183 |
|
FVECT dir; |
184 |
< |
float rt_buf[6]; |
184 |
> |
float rt_buf[12]; |
185 |
|
double res; |
186 |
|
|
187 |
|
if (compdir(dir, vh, vv) < 0) |
189 |
|
npixinvw++; |
190 |
|
if ((res = pict_val(dir)) >= 0.0) |
191 |
|
return(res); |
192 |
< |
if (rt_pid == -1) { |
192 |
> |
if (rt_pd.r == -1) { |
193 |
|
npixmiss++; |
194 |
|
return(-1.0); |
195 |
|
} |
221 |
|
#endif |
222 |
|
n = 0; |
223 |
|
for (vh = -hsize; vh <= hsize; vh++) { |
224 |
< |
if (compdir(dir, vh, vv) < 0) { /* off viewable region */ |
224 |
> |
if (compdir(dir, vh, vv) < 0) { /* not in view */ |
225 |
|
vb[vh+hsize] = -1.0; |
226 |
|
continue; |
227 |
|
} |
228 |
|
npixinvw++; |
229 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
230 |
|
continue; |
231 |
< |
if (rt_pid == -1) { /* missing information */ |
231 |
> |
if (rt_pd.r == -1) { /* missing information */ |
232 |
|
npixmiss++; |
233 |
|
continue; |
234 |
|
} |
235 |
|
/* send to rtrace */ |
236 |
< |
if (n >= MAXPIX) { /* flush */ |
236 |
> |
if (n >= maxpix) { /* flush */ |
237 |
|
rt_compute(rt_buf, n); |
238 |
|
while (n-- > 0) |
239 |
|
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
262 |
|
float *pb; |
263 |
|
int np; |
264 |
|
{ |
259 |
– |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
260 |
– |
|
265 |
|
#ifdef DEBUG |
266 |
|
if (verbose && np > 1) |
267 |
|
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
268 |
|
progname, np); |
269 |
|
#endif |
270 |
< |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
271 |
< |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
272 |
< |
fprintf(stderr, "%s: error writing to rtrace process\n", |
270 |
> |
memset(pb+6*np, '\0', 6*sizeof(float)); |
271 |
> |
if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1), |
272 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { |
273 |
> |
fprintf(stderr, "%s: rtrace communication error\n", |
274 |
|
progname); |
275 |
|
exit(1); |
276 |
|
} |
272 |
– |
if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np) |
273 |
– |
< 3*sizeof(float)*np) { |
274 |
– |
fprintf(stderr, "%s: error reading from rtrace process\n", |
275 |
– |
progname); |
276 |
– |
exit(1); |
277 |
– |
} |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
+ |
int |
281 |
|
getexpos(s) /* get exposure from header line */ |
282 |
|
char *s; |
283 |
|
{ |
284 |
+ |
char fmt[32]; |
285 |
+ |
|
286 |
|
if (isexpos(s)) |
287 |
|
exposure *= exposval(s); |
288 |
+ |
else if (isformat(s)) { |
289 |
+ |
formatval(fmt, s); |
290 |
+ |
wrongformat = strcmp(fmt, COLRFMT); |
291 |
+ |
} |
292 |
+ |
return(0); |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
|
open_pict(fn) /* open picture file */ |
297 |
|
char *fn; |
298 |
|
{ |
292 |
– |
register int i; |
293 |
– |
|
299 |
|
if ((pictfp = fopen(fn, "r")) == NULL) { |
300 |
< |
fprintf("%s: cannot open\n", fn); |
300 |
> |
fprintf(stderr, "%s: cannot open\n", fn); |
301 |
|
exit(1); |
302 |
|
} |
303 |
|
exposure = 1.0; |
304 |
< |
getheader(pictfp, getexpos); |
305 |
< |
if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
306 |
< |
fprintf("%s: bad picture resolution\n", fn); |
304 |
> |
getheader(pictfp, getexpos, NULL); |
305 |
> |
if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { |
306 |
> |
fprintf(stderr, "%s: incompatible picture format\n", fn); |
307 |
|
exit(1); |
308 |
|
} |
309 |
|
initscans(); |
312 |
|
|
313 |
|
close_pict() /* done with picture */ |
314 |
|
{ |
310 |
– |
register int i; |
311 |
– |
|
315 |
|
if (pictfp == NULL) |
316 |
|
return; |
317 |
|
fclose(pictfp); |
323 |
|
fork_rtrace(av) /* open pipe and start rtrace */ |
324 |
|
char *av[]; |
325 |
|
{ |
326 |
< |
int p0[2], p1[2]; |
326 |
> |
int rval; |
327 |
|
|
328 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
328 |
> |
rval = open_process(&rt_pd, av); |
329 |
> |
if (rval < 0) { |
330 |
|
perror(progname); |
331 |
|
exit(1); |
332 |
|
} |
333 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
334 |
< |
close(p0[1]); |
331 |
< |
close(p1[0]); |
332 |
< |
if (p0[0] != 0) { /* connect p0 to stdin */ |
333 |
< |
dup2(p0[0], 0); |
334 |
< |
close(p0[0]); |
335 |
< |
} |
336 |
< |
if (p1[1] != 0) { /* connect p1 to stdout */ |
337 |
< |
dup2(p1[1], 1); |
338 |
< |
close(p1[1]); |
339 |
< |
} |
340 |
< |
execvp(av[0], av); |
341 |
< |
perror(av[0]); |
342 |
< |
_exit(127); |
343 |
< |
} |
344 |
< |
if (rt_pid == -1) { |
345 |
< |
perror(progname); |
333 |
> |
if (rval == 0) { |
334 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
335 |
|
exit(1); |
336 |
|
} |
337 |
< |
close(p0[0]); |
338 |
< |
close(p1[1]); |
339 |
< |
fd_tort = p0[1]; |
340 |
< |
fd_fromrt = p1[0]; |
337 |
> |
maxpix = rval/(6*sizeof(float)); |
338 |
> |
if (maxpix > MAXPIX) |
339 |
> |
maxpix = MAXPIX; |
340 |
> |
maxpix--; |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
done_rtrace() /* wait for rtrace to finish */ |
345 |
|
{ |
346 |
< |
int pid, status; |
346 |
> |
int status; |
347 |
|
|
348 |
< |
if (rt_pid == -1) |
349 |
< |
return; |
361 |
< |
close(fd_tort); |
362 |
< |
close(fd_fromrt); |
363 |
< |
while ((pid = wait(&status)) != -1 && pid != rt_pid) |
364 |
< |
; |
365 |
< |
if (pid == rt_pid && status != 0) { |
348 |
> |
status = close_process(&rt_pd); |
349 |
> |
if (status > 0) { |
350 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
351 |
|
progname, status); |
352 |
|
exit(1); |
353 |
|
} |
354 |
< |
rt_pid = -1; |
354 |
> |
rt_pd.r = -1; |
355 |
|
} |
356 |
|
|
357 |
|
|
374 |
– |
int |
375 |
– |
readbuf(fd, bpos, siz) |
376 |
– |
int fd; |
377 |
– |
char *bpos; |
378 |
– |
int siz; |
379 |
– |
{ |
380 |
– |
register int cc, nrem = siz; |
381 |
– |
|
382 |
– |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
383 |
– |
bpos += cc; |
384 |
– |
nrem -= cc; |
385 |
– |
} |
386 |
– |
if (cc < 0) |
387 |
– |
return(cc); |
388 |
– |
return(siz-nrem); |
389 |
– |
} |
390 |
– |
|
391 |
– |
|
392 |
– |
int |
393 |
– |
writebuf(fd, bpos, siz) |
394 |
– |
char *bpos; |
395 |
– |
int siz; |
396 |
– |
{ |
397 |
– |
register int cc, nrem = siz; |
398 |
– |
|
399 |
– |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
400 |
– |
bpos += cc; |
401 |
– |
nrem -= cc; |
402 |
– |
} |
403 |
– |
if (cc < 0) |
404 |
– |
return(cc); |
405 |
– |
return(siz-nrem); |
406 |
– |
} |
407 |
– |
|
408 |
– |
|
358 |
|
SCAN * |
359 |
|
scanretire() /* retire old scanlines to free list */ |
360 |
|
{ |
361 |
|
SCAN *sold[NRETIRE]; |
413 |
– |
SCAN head; |
362 |
|
int n; |
363 |
|
int h; |
364 |
|
register SCAN *sl; |
365 |
|
register int i; |
366 |
|
/* grab the NRETIRE oldest scanlines */ |
367 |
|
sold[n = 0] = NULL; |
368 |
< |
for (h = 0; h < HSIZE; h++) { |
369 |
< |
head.next = hashtab[h]; |
370 |
< |
sl = &head; |
371 |
< |
while (sl->next != NULL) { |
424 |
< |
for (i = n; i && sold[i-1]->lused > sl->next->lused; i--) |
425 |
< |
if (i == NRETIRE) { /* reallocate */ |
426 |
< |
register int oh; |
427 |
< |
oh = shash(sold[NRETIRE-1]->y); |
428 |
< |
sold[NRETIRE-1]->next = hashtab[oh]; |
429 |
< |
if (h == oh) { |
430 |
< |
head.next = sold[NRETIRE-1]; |
431 |
< |
if (sl == &head) |
432 |
< |
sl = head.next; |
433 |
< |
} else |
434 |
< |
hashtab[oh] = sold[NRETIRE-1]; |
435 |
< |
} else /* else bubble up */ |
368 |
> |
for (h = 0; h < HSIZE; h++) |
369 |
> |
for (sl = hashtab[h]; sl != NULL; sl = sl->next) { |
370 |
> |
for (i = n; i && sold[i-1]->lused > sl->lused; i--) |
371 |
> |
if (i < NRETIRE) |
372 |
|
sold[i] = sold[i-1]; |
373 |
|
if (i < NRETIRE) { |
374 |
< |
sold[i] = sl->next; |
439 |
< |
sl->next = sl->next->next; |
374 |
> |
sold[i] = sl; |
375 |
|
if (n < NRETIRE) /* grow list */ |
376 |
|
n++; |
377 |
< |
} else |
377 |
> |
} |
378 |
> |
} |
379 |
> |
/* put scanlines into free list */ |
380 |
> |
for (i = 0; i < n; i++) { |
381 |
> |
h = shash(sold[i]->y); |
382 |
> |
sl = hashtab[h]; |
383 |
> |
if (sl == sold[i]) |
384 |
> |
hashtab[h] = sl->next; |
385 |
> |
else { |
386 |
> |
while (sl->next != sold[i]) /* IS in list */ |
387 |
|
sl = sl->next; |
388 |
+ |
sl->next = sold[i]->next; |
389 |
|
} |
390 |
< |
hashtab[h] = head.next; |
390 |
> |
if (i > 0) { /* save oldest as return value */ |
391 |
> |
sold[i]->next = freelist; |
392 |
> |
freelist = sold[i]; |
393 |
> |
} |
394 |
|
} |
447 |
– |
/* put scanlines into free list */ |
448 |
– |
for (i = 1; i < n; i++) { |
449 |
– |
sold[i]->next = freelist; |
450 |
– |
freelist = sold[i]; |
451 |
– |
} |
395 |
|
return(sold[0]); |
396 |
|
} |
397 |
|
|
405 |
|
register SCAN *ptr; |
406 |
|
register int i; |
407 |
|
/* initialize positions */ |
408 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
408 |
> |
scanpos = (long *)bmalloc(pysiz*sizeof(long)); |
409 |
|
if (scanpos == NULL) |
410 |
|
memerr("scanline positions"); |
411 |
|
for (i = pysiz-1; i >= 0; i--) |
416 |
|
hashtab[i] = NULL; |
417 |
|
/* allocate scanline buffers */ |
418 |
|
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
419 |
< |
#ifdef ALIGN |
420 |
< |
scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1); |
419 |
> |
#ifdef ALIGNT |
420 |
> |
scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1); |
421 |
|
#endif |
422 |
|
i = MAXSBUF / scansize; /* compute number to allocate */ |
423 |
|
if (i > HSIZE) |
424 |
|
i = HSIZE; |
425 |
< |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
425 |
> |
scanbufsiz = i*scansize; |
426 |
> |
scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ |
427 |
|
if (scan_buf == NULL) |
428 |
|
memerr("scanline buffers"); |
429 |
|
ptr = (SCAN *)scan_buf; |
440 |
|
|
441 |
|
donescans() /* free up scanlines */ |
442 |
|
{ |
443 |
< |
free(scan_buf); |
444 |
< |
free((char *)scanpos); |
443 |
> |
bfree(scan_buf, scanbufsiz); |
444 |
> |
bfree((char *)scanpos, pysiz*sizeof(long)); |
445 |
|
} |