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 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(); |
55 |
> |
static SCAN * claimscan(int y); |
56 |
> |
static COLR * getpictscan(int y); |
57 |
> |
static double pict_val(FVECT vd); |
58 |
> |
static void rt_compute(float *pb, int np); |
59 |
> |
static gethfunc getexpos; |
60 |
> |
static SCAN * scanretire(void); |
61 |
> |
static void initscans(void); |
62 |
> |
static void donescans(void); |
63 |
|
|
58 |
– |
extern long ftell(); |
64 |
|
|
65 |
< |
|
66 |
< |
SCAN * |
67 |
< |
claimscan(y) /* claim scanline from buffers */ |
68 |
< |
int y; |
65 |
> |
static SCAN * |
66 |
> |
claimscan( /* claim scanline from buffers */ |
67 |
> |
int y |
68 |
> |
) |
69 |
|
{ |
70 |
|
int hi = shash(y); |
71 |
|
SCAN *slast; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
< |
COLR * |
95 |
< |
getpictscan(y) /* get picture scanline */ |
96 |
< |
int y; |
94 |
> |
static COLR * |
95 |
> |
getpictscan( /* get picture scanline */ |
96 |
> |
int y |
97 |
> |
) |
98 |
|
{ |
99 |
|
register SCAN *sl; |
100 |
|
register int i; |
144 |
|
|
145 |
|
|
146 |
|
#ifdef DEBUG |
147 |
< |
pict_stats() /* print out picture read statistics */ |
147 |
> |
static void |
148 |
> |
pict_stats(void) /* print out picture read statistics */ |
149 |
|
{ |
150 |
|
static long lastcall = 0L; /* ncall at last report */ |
151 |
|
static long lastread = 0L; /* nread at last report */ |
162 |
|
#endif |
163 |
|
|
164 |
|
|
165 |
< |
double |
166 |
< |
pict_val(vd) /* find picture value for view direction */ |
167 |
< |
FVECT vd; |
165 |
> |
static double |
166 |
> |
pict_val( /* find picture value for view direction */ |
167 |
> |
FVECT vd |
168 |
> |
) |
169 |
|
{ |
170 |
|
FVECT pp; |
171 |
< |
double vpx, vpy, vpz; |
171 |
> |
FVECT ip; |
172 |
|
COLOR res; |
173 |
|
|
174 |
|
if (pictfp == NULL) |
176 |
|
pp[0] = pictview.vp[0] + vd[0]; |
177 |
|
pp[1] = pictview.vp[1] + vd[1]; |
178 |
|
pp[2] = pictview.vp[2] + vd[2]; |
179 |
< |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
180 |
< |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
179 |
> |
viewloc(ip, &pictview, pp); |
180 |
> |
if (ip[2] <= FTINY || ip[0] < 0. || ip[0] >= 1. || |
181 |
> |
ip[1] < 0. || ip[1] >= 1.) |
182 |
|
return(-1.0); |
183 |
< |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
183 |
> |
colr_color(res, getpictscan((int)(ip[1]*pysiz))[(int)(ip[0]*pxsiz)]); |
184 |
|
return(luminance(res)/exposure); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
< |
double |
189 |
< |
getviewpix(vh, vv) /* compute single view pixel */ |
190 |
< |
int vh, vv; |
188 |
> |
extern double |
189 |
> |
getviewpix( /* compute single view pixel */ |
190 |
> |
int vh, |
191 |
> |
int vv |
192 |
> |
) |
193 |
|
{ |
194 |
|
FVECT dir; |
195 |
< |
float rt_buf[6]; |
195 |
> |
float rt_buf[12]; |
196 |
|
double res; |
197 |
|
|
198 |
|
if (compdir(dir, vh, vv) < 0) |
200 |
|
npixinvw++; |
201 |
|
if ((res = pict_val(dir)) >= 0.0) |
202 |
|
return(res); |
203 |
< |
if (rt_pid == -1) { |
203 |
> |
if (rt_pd.r == -1) { |
204 |
|
npixmiss++; |
205 |
|
return(-1.0); |
206 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
< |
getviewspan(vv, vb) /* compute a span of view pixels */ |
219 |
< |
int vv; |
220 |
< |
float *vb; |
218 |
> |
extern void |
219 |
> |
getviewspan( /* compute a span of view pixels */ |
220 |
> |
int vv, |
221 |
> |
float *vb |
222 |
> |
) |
223 |
|
{ |
224 |
|
float rt_buf[6*MAXPIX]; /* rtrace send/receive buffer */ |
225 |
|
register int n; /* number of pixels in buffer */ |
241 |
|
npixinvw++; |
242 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
243 |
|
continue; |
244 |
< |
if (rt_pid == -1) { /* missing information */ |
244 |
> |
if (rt_pd.r == -1) { /* missing information */ |
245 |
|
npixmiss++; |
246 |
|
continue; |
247 |
|
} |
248 |
|
/* send to rtrace */ |
249 |
< |
if (n >= MAXPIX) { /* flush */ |
249 |
> |
if (n >= maxpix) { /* flush */ |
250 |
|
rt_compute(rt_buf, n); |
251 |
|
while (n-- > 0) |
252 |
|
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
< |
rt_compute(pb, np) /* process buffer through rtrace */ |
275 |
< |
float *pb; |
276 |
< |
int np; |
274 |
> |
static void |
275 |
> |
rt_compute( /* process buffer through rtrace */ |
276 |
> |
float *pb, |
277 |
> |
int np |
278 |
> |
) |
279 |
|
{ |
265 |
– |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
266 |
– |
|
280 |
|
#ifdef DEBUG |
281 |
|
if (verbose && np > 1) |
282 |
|
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
283 |
|
progname, np); |
284 |
|
#endif |
285 |
< |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
286 |
< |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
287 |
< |
fprintf(stderr, "%s: error writing to rtrace process\n", |
285 |
> |
memset(pb+6*np, '\0', 6*sizeof(float)); |
286 |
> |
if (process(&rt_pd, (char *)pb, (char *)pb, 3*sizeof(float)*(np+1), |
287 |
> |
6*sizeof(float)*(np+1)) < 3*sizeof(float)*(np+1)) { |
288 |
> |
fprintf(stderr, "%s: rtrace communication error\n", |
289 |
|
progname); |
290 |
|
exit(1); |
291 |
|
} |
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 |
– |
} |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
< |
getexpos(s) /* get exposure from header line */ |
296 |
< |
char *s; |
295 |
> |
static int |
296 |
> |
getexpos( /* get exposure from header line */ |
297 |
> |
char *s, |
298 |
> |
void *p |
299 |
> |
) |
300 |
|
{ |
301 |
|
char fmt[32]; |
302 |
|
|
306 |
|
formatval(fmt, s); |
307 |
|
wrongformat = strcmp(fmt, COLRFMT); |
308 |
|
} |
309 |
+ |
return(0); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
< |
open_pict(fn) /* open picture file */ |
314 |
< |
char *fn; |
313 |
> |
extern void |
314 |
> |
open_pict( /* open picture file */ |
315 |
> |
char *fn |
316 |
> |
) |
317 |
|
{ |
318 |
|
if ((pictfp = fopen(fn, "r")) == NULL) { |
319 |
< |
fprintf("%s: cannot open\n", fn); |
319 |
> |
fprintf(stderr, "%s: cannot open\n", fn); |
320 |
|
exit(1); |
321 |
|
} |
322 |
|
exposure = 1.0; |
323 |
|
getheader(pictfp, getexpos, NULL); |
324 |
< |
if (wrongformat || |
325 |
< |
fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
312 |
< |
fprintf("%s: bad picture format\n", fn); |
324 |
> |
if (wrongformat || !fscnresolu(&pxsiz, &pysiz, pictfp)) { |
325 |
> |
fprintf(stderr, "%s: incompatible picture format\n", fn); |
326 |
|
exit(1); |
327 |
|
} |
328 |
|
initscans(); |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
< |
close_pict() /* done with picture */ |
332 |
> |
extern void |
333 |
> |
close_pict(void) /* done with picture */ |
334 |
|
{ |
335 |
|
if (pictfp == NULL) |
336 |
|
return; |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
< |
fork_rtrace(av) /* open pipe and start rtrace */ |
344 |
< |
char *av[]; |
343 |
> |
extern void |
344 |
> |
fork_rtrace( /* open pipe and start rtrace */ |
345 |
> |
char *av[] |
346 |
> |
) |
347 |
|
{ |
348 |
< |
int p0[2], p1[2]; |
348 |
> |
int rval; |
349 |
|
|
350 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
350 |
> |
rval = open_process(&rt_pd, av); |
351 |
> |
if (rval < 0) { |
352 |
|
perror(progname); |
353 |
|
exit(1); |
354 |
|
} |
355 |
< |
if ((rt_pid = vfork()) == 0) { /* if child */ |
356 |
< |
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); |
355 |
> |
if (rval == 0) { |
356 |
> |
fprintf(stderr, "%s: command not found\n", av[0]); |
357 |
|
exit(1); |
358 |
|
} |
359 |
< |
close(p0[0]); |
360 |
< |
close(p1[1]); |
361 |
< |
fd_tort = p0[1]; |
362 |
< |
fd_fromrt = p1[0]; |
359 |
> |
maxpix = rval/(6*sizeof(float)); |
360 |
> |
if (maxpix > MAXPIX) |
361 |
> |
maxpix = MAXPIX; |
362 |
> |
maxpix--; |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
< |
done_rtrace() /* wait for rtrace to finish */ |
366 |
> |
extern void |
367 |
> |
done_rtrace(void) /* wait for rtrace to finish */ |
368 |
|
{ |
369 |
< |
int pid, status; |
369 |
> |
int status; |
370 |
|
|
371 |
< |
if (rt_pid == -1) |
372 |
< |
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) { |
371 |
> |
status = close_process(&rt_pd); |
372 |
> |
if (status > 0) { |
373 |
|
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
374 |
|
progname, status); |
375 |
|
exit(1); |
376 |
|
} |
377 |
< |
rt_pid = -1; |
377 |
> |
rt_pd.r = -1; |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
< |
int |
382 |
< |
readbuf(fd, bpos, siz) |
385 |
< |
int fd; |
386 |
< |
char *bpos; |
387 |
< |
int siz; |
381 |
> |
static SCAN * |
382 |
> |
scanretire(void) /* retire old scanlines to free list */ |
383 |
|
{ |
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 |
– |
|
418 |
– |
SCAN * |
419 |
– |
scanretire() /* retire old scanlines to free list */ |
420 |
– |
{ |
384 |
|
SCAN *sold[NRETIRE]; |
385 |
|
int n; |
386 |
|
int h; |
422 |
|
static char *scan_buf; |
423 |
|
|
424 |
|
|
425 |
< |
initscans() /* initialize scanline buffers */ |
425 |
> |
static void |
426 |
> |
initscans(void) /* initialize scanline buffers */ |
427 |
|
{ |
428 |
|
int scansize; |
429 |
|
register SCAN *ptr; |
430 |
|
register int i; |
431 |
|
/* initialize positions */ |
432 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
432 |
> |
scanpos = (long *)bmalloc(pysiz*sizeof(long)); |
433 |
|
if (scanpos == NULL) |
434 |
|
memerr("scanline positions"); |
435 |
|
for (i = pysiz-1; i >= 0; i--) |
440 |
|
hashtab[i] = NULL; |
441 |
|
/* allocate scanline buffers */ |
442 |
|
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
443 |
< |
#ifdef ALIGN |
444 |
< |
scansize = scansize+(sizeof(ALIGN)-1) & ~(sizeof(ALIGN)-1); |
443 |
> |
#ifdef ALIGNT |
444 |
> |
scansize = scansize+(sizeof(ALIGNT)-1) & ~(sizeof(ALIGNT)-1); |
445 |
|
#endif |
446 |
|
i = MAXSBUF / scansize; /* compute number to allocate */ |
447 |
|
if (i > HSIZE) |
448 |
|
i = HSIZE; |
449 |
< |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
449 |
> |
scanbufsiz = i*scansize; |
450 |
> |
scan_buf = bmalloc(scanbufsiz); /* get in one big chunk */ |
451 |
|
if (scan_buf == NULL) |
452 |
|
memerr("scanline buffers"); |
453 |
|
ptr = (SCAN *)scan_buf; |
462 |
|
} |
463 |
|
|
464 |
|
|
465 |
< |
donescans() /* free up scanlines */ |
465 |
> |
static void |
466 |
> |
donescans(void) /* free up scanlines */ |
467 |
|
{ |
468 |
< |
free(scan_buf); |
469 |
< |
free((char *)scanpos); |
468 |
> |
bfree(scan_buf, scanbufsiz); |
469 |
> |
bfree((char *)scanpos, pysiz*sizeof(long)); |
470 |
|
} |