1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Compute pixels for glare calculation |
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) |
18 |
|
19 |
#ifndef BSD |
20 |
#define vfork fork |
21 |
#endif |
22 |
|
23 |
#define NSCANS 64 /* number of scanlines to buffer */ |
24 |
|
25 |
int rt_pid = -1; /* process id for rtrace */ |
26 |
int fd_tort, fd_fromrt; /* pipe descriptors */ |
27 |
|
28 |
FILE *pictfp = NULL; /* picture file pointer */ |
29 |
double exposure; /* picture exposure */ |
30 |
int pxsiz, pysiz; /* picture dimensions */ |
31 |
int curpos; /* current scanline */ |
32 |
long *scanpos; /* scanline positions */ |
33 |
struct { |
34 |
long lused; /* for LRU replacement */ |
35 |
int y; /* scanline position */ |
36 |
COLR *sl; /* scanline contents */ |
37 |
} scan[NSCANS]; /* buffered scanlines */ |
38 |
|
39 |
static long ncall = 0L; /* number of calls to getpictscan */ |
40 |
static long nread = 0L; /* number of scanlines read */ |
41 |
|
42 |
extern long ftell(); |
43 |
|
44 |
|
45 |
COLR * |
46 |
getpictscan(y) /* get picture scanline */ |
47 |
int y; |
48 |
{ |
49 |
int minused; |
50 |
register int i; |
51 |
/* first check our buffers */ |
52 |
ncall++; |
53 |
minused = 0; |
54 |
for (i = 0; i < NSCANS; i++) { |
55 |
if (scan[i].y == y) { |
56 |
scan[i].lused = ncall; |
57 |
return(scan[i].sl); |
58 |
} |
59 |
if (scan[i].lused < scan[minused].lused) |
60 |
minused = i; |
61 |
} |
62 |
/* not there, read it in */ |
63 |
if (scanpos[y] < 0) { /* need to search */ |
64 |
for (i = y+1; i < curpos; i++) |
65 |
if (scanpos[i] >= 0) { |
66 |
if (fseek(pictfp, scanpos[i], 0) < 0) |
67 |
goto seekerr; |
68 |
curpos = i; |
69 |
break; |
70 |
} |
71 |
while (curpos >= y) { |
72 |
scanpos[curpos] = ftell(pictfp); |
73 |
if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) |
74 |
goto readerr; |
75 |
nread++; |
76 |
curpos--; |
77 |
} |
78 |
} else { |
79 |
if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0) |
80 |
goto seekerr; |
81 |
if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) |
82 |
goto readerr; |
83 |
nread++; |
84 |
curpos = y-1; |
85 |
} |
86 |
scan[minused].lused = ncall; |
87 |
scan[minused].y = y; |
88 |
return(scan[minused].sl); |
89 |
readerr: |
90 |
fprintf(stderr, "%s: picture read error\n", progname); |
91 |
exit(1); |
92 |
seekerr: |
93 |
fprintf(stderr, "%s: picture seek error\n", progname); |
94 |
exit(1); |
95 |
} |
96 |
|
97 |
|
98 |
#ifdef DEBUG |
99 |
pict_stats() /* print out picture read statistics */ |
100 |
{ |
101 |
static long lastcall = 0L; /* ncall at last report */ |
102 |
static long lastread = 0L; /* nread at last report */ |
103 |
|
104 |
if (ncall == lastcall) |
105 |
return; |
106 |
fprintf(stderr, "%s: %ld scanlines read in %ld calls\n", |
107 |
progname, nread-lastread, ncall-lastcall); |
108 |
lastcall = ncall; |
109 |
lastread = nread; |
110 |
} |
111 |
#endif |
112 |
|
113 |
|
114 |
double |
115 |
pict_val(vd) /* find picture value for view direction */ |
116 |
FVECT vd; |
117 |
{ |
118 |
FVECT pp; |
119 |
double vpx, vpy, vpz; |
120 |
COLOR res; |
121 |
|
122 |
if (pictfp == NULL) |
123 |
return(-1.0); |
124 |
pp[0] = pictview.vp[0] + vd[0]; |
125 |
pp[1] = pictview.vp[1] + vd[1]; |
126 |
pp[2] = pictview.vp[2] + vd[2]; |
127 |
viewpixel(&vpx, &vpy, &vpz, &pictview, pp); |
128 |
if (vpz <= FTINY || vpx < 0. || vpx >= 1. || vpy < 0. || vpy >= 1.) |
129 |
return(-1.0); |
130 |
colr_color(res, getpictscan((int)(vpy*pysiz))[(int)(vpx*pxsiz)]); |
131 |
return(luminance(res)/exposure); |
132 |
} |
133 |
|
134 |
|
135 |
double |
136 |
getviewpix(vh, vv) /* compute single view pixel */ |
137 |
int vh, vv; |
138 |
{ |
139 |
FVECT dir; |
140 |
float rt_buf[6]; |
141 |
double res; |
142 |
|
143 |
if (compdir(dir, vh, vv) < 0) |
144 |
return(-1.0); |
145 |
if ((res = pict_val(dir)) >= 0.0) |
146 |
return(res); |
147 |
if (rt_pid == -1) |
148 |
return(-1.0); |
149 |
rt_buf[0] = ourview.vp[0]; |
150 |
rt_buf[1] = ourview.vp[1]; |
151 |
rt_buf[2] = ourview.vp[2]; |
152 |
rt_buf[3] = dir[0]; |
153 |
rt_buf[4] = dir[1]; |
154 |
rt_buf[5] = dir[2]; |
155 |
rt_compute(rt_buf, 1); |
156 |
return(luminance(rt_buf)); |
157 |
} |
158 |
|
159 |
|
160 |
getviewspan(vv, vb) /* compute a span of view pixels */ |
161 |
int vv; |
162 |
float *vb; |
163 |
{ |
164 |
float rt_buf[6*MAXPIX]; /* rtrace send/receive buffer */ |
165 |
register int n; /* number of pixels in buffer */ |
166 |
short buf_vh[MAXPIX]; /* pixel positions */ |
167 |
FVECT dir; |
168 |
register int vh; |
169 |
|
170 |
#ifdef DEBUG |
171 |
if (verbose) |
172 |
fprintf(stderr, "%s: computing view span at %d...\n", |
173 |
progname, vv); |
174 |
#endif |
175 |
n = 0; |
176 |
for (vh = -hsize; vh <= hsize; vh++) { |
177 |
if (compdir(dir, vh, vv) < 0) { /* off viewable region */ |
178 |
vb[vh+hsize] = -1.0; |
179 |
continue; |
180 |
} |
181 |
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
182 |
continue; |
183 |
if (rt_pid == -1) /* missing information */ |
184 |
continue; |
185 |
/* send to rtrace */ |
186 |
if (n >= MAXPIX) { /* flush */ |
187 |
rt_compute(rt_buf, n); |
188 |
while (n-- > 0) |
189 |
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
190 |
} |
191 |
rt_buf[6*n] = ourview.vp[0]; |
192 |
rt_buf[6*n+1] = ourview.vp[1]; |
193 |
rt_buf[6*n+2] = ourview.vp[2]; |
194 |
rt_buf[6*n+3] = dir[0]; |
195 |
rt_buf[6*n+4] = dir[1]; |
196 |
rt_buf[6*n+5] = dir[2]; |
197 |
buf_vh[n++] = vh; |
198 |
} |
199 |
#ifdef DEBUG |
200 |
if (verbose) |
201 |
pict_stats(); |
202 |
#endif |
203 |
if (n > 0) { /* process pending buffer */ |
204 |
rt_compute(rt_buf, n); |
205 |
while (n-- > 0) |
206 |
vb[buf_vh[n]+hsize] = luminance(rt_buf+3*n); |
207 |
} |
208 |
} |
209 |
|
210 |
|
211 |
rt_compute(pb, np) /* process buffer through rtrace */ |
212 |
float *pb; |
213 |
int np; |
214 |
{ |
215 |
static float nbuf[6] = {0.,0.,0.,0.,0.,0.}; |
216 |
|
217 |
#ifdef DEBUG |
218 |
if (verbose && np > 1) |
219 |
fprintf(stderr, "%s: sending %d samples to rtrace...\n", |
220 |
progname, np); |
221 |
#endif |
222 |
if (writebuf(fd_tort,(char *)pb,6*sizeof(float)*np) < 6*sizeof(float)*np |
223 |
|| writebuf(fd_tort,(char *)nbuf,sizeof(nbuf)) < sizeof(nbuf)) { |
224 |
fprintf(stderr, "%s: error writing to rtrace process\n", |
225 |
progname); |
226 |
exit(1); |
227 |
} |
228 |
if (readbuf(fd_fromrt, (char *)pb, 3*sizeof(float)*np) |
229 |
< 3*sizeof(float)*np) { |
230 |
fprintf(stderr, "%s: error reading from rtrace process\n", |
231 |
progname); |
232 |
exit(1); |
233 |
} |
234 |
} |
235 |
|
236 |
|
237 |
getexpos(s) /* get exposure from header line */ |
238 |
char *s; |
239 |
{ |
240 |
if (isexpos(s)) |
241 |
exposure *= exposval(s); |
242 |
} |
243 |
|
244 |
|
245 |
open_pict(fn) /* open picture file */ |
246 |
char *fn; |
247 |
{ |
248 |
register int i; |
249 |
|
250 |
if ((pictfp = fopen(fn, "r")) == NULL) { |
251 |
fprintf("%s: cannot open\n", fn); |
252 |
exit(1); |
253 |
} |
254 |
exposure = 1.0; |
255 |
getheader(pictfp, getexpos); |
256 |
if (fgetresolu(&pxsiz, &pysiz, pictfp) != (YMAJOR|YDECR)) { |
257 |
fprintf("%s: bad picture resolution\n", fn); |
258 |
exit(1); |
259 |
} |
260 |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
261 |
if (scanpos == NULL) |
262 |
memerr("scanline positions"); |
263 |
for (i = pysiz-1; i >= 0; i--) |
264 |
scanpos[i] = -1L; |
265 |
curpos = pysiz-1; |
266 |
for (i = 0; i < NSCANS; i++) { |
267 |
scan[i].lused = -1; |
268 |
scan[i].y = -1; |
269 |
scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR)); |
270 |
if (scan[i].sl == NULL) |
271 |
memerr("scanline buffers"); |
272 |
} |
273 |
} |
274 |
|
275 |
|
276 |
close_pict() /* done with picture */ |
277 |
{ |
278 |
register int i; |
279 |
|
280 |
if (pictfp == NULL) |
281 |
return; |
282 |
fclose(pictfp); |
283 |
free((char *)scanpos); |
284 |
for (i = 0; i < NSCANS; i++) |
285 |
free((char *)scan[i].sl); |
286 |
pictfp = NULL; |
287 |
} |
288 |
|
289 |
|
290 |
fork_rtrace(av) /* open pipe and start rtrace */ |
291 |
char *av[]; |
292 |
{ |
293 |
int p0[2], p1[2]; |
294 |
|
295 |
if (pipe(p0) < 0 || pipe(p1) < 0) { |
296 |
perror(progname); |
297 |
exit(1); |
298 |
} |
299 |
if ((rt_pid = vfork()) == 0) { /* if child */ |
300 |
close(p0[1]); |
301 |
close(p1[0]); |
302 |
if (p0[0] != 0) { /* connect p0 to stdin */ |
303 |
dup2(p0[0], 0); |
304 |
close(p0[0]); |
305 |
} |
306 |
if (p1[1] != 0) { /* connect p1 to stdout */ |
307 |
dup2(p1[1], 1); |
308 |
close(p1[1]); |
309 |
} |
310 |
execvp(av[0], av); |
311 |
perror(av[0]); |
312 |
_exit(127); |
313 |
} |
314 |
if (rt_pid == -1) { |
315 |
perror(progname); |
316 |
exit(1); |
317 |
} |
318 |
close(p0[0]); |
319 |
close(p1[1]); |
320 |
fd_tort = p0[1]; |
321 |
fd_fromrt = p1[0]; |
322 |
} |
323 |
|
324 |
|
325 |
done_rtrace() /* wait for rtrace to finish */ |
326 |
{ |
327 |
int pid, status; |
328 |
|
329 |
if (rt_pid == -1) |
330 |
return; |
331 |
close(fd_tort); |
332 |
close(fd_fromrt); |
333 |
while ((pid = wait(&status)) != -1 && pid != rt_pid) |
334 |
; |
335 |
if (pid == rt_pid && status != 0) { |
336 |
fprintf(stderr, "%s: bad status (%d) from rtrace\n", |
337 |
progname, status); |
338 |
exit(1); |
339 |
} |
340 |
rt_pid = -1; |
341 |
} |
342 |
|
343 |
|
344 |
int |
345 |
readbuf(fd, bpos, siz) |
346 |
int fd; |
347 |
char *bpos; |
348 |
int siz; |
349 |
{ |
350 |
register int cc, nrem = siz; |
351 |
|
352 |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
353 |
bpos += cc; |
354 |
nrem -= cc; |
355 |
} |
356 |
if (cc < 0) |
357 |
return(cc); |
358 |
return(siz-nrem); |
359 |
} |
360 |
|
361 |
|
362 |
int |
363 |
writebuf(fd, bpos, siz) |
364 |
char *bpos; |
365 |
int siz; |
366 |
{ |
367 |
register int cc, nrem = siz; |
368 |
|
369 |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
370 |
bpos += cc; |
371 |
nrem -= cc; |
372 |
} |
373 |
if (cc < 0) |
374 |
return(cc); |
375 |
return(siz-nrem); |
376 |
} |