ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glareval.c
Revision: 1.4
Committed: Wed Mar 20 13:20:06 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +6 -0 lines
Log Message:
reduced volume of verbose reporting except when DEBUG defined

File Contents

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