ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.11
Committed: Thu Nov 12 15:06:00 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.10: +42 -19 lines
Log Message:
change clumpbeams() callback function to take clump's queue

File Contents

# Content
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Copy data into a holodeck file
9 */
10
11 #include "holo.h"
12 #include "view.h"
13 #include "resolu.h"
14
15 #ifndef BKBSIZE
16 #define BKBSIZE 256 /* beam clump size (kilobytes) */
17 #endif
18
19 int checkdepth = 1; /* check depth (!-f option)? */
20 int checkrepeats = 0; /* check for repeats (-c option)? */
21 int frompicz; /* input from pictures & depth-buffers? */
22 int noutsects; /* number of output sections */
23 char obstr, unobstr; /* flag pointer values */
24
25 char *progname; /* global argv[0] */
26
27
28 main(argc, argv)
29 int argc;
30 char *argv[];
31 {
32 int i;
33
34 progname = argv[0];
35 frompicz = -1;
36 for (i = 2; i < argc && argv[i][0] == '-'; i++)
37 switch (argv[i][1]) {
38 case 'u':
39 checkrepeats = 1;
40 break;
41 case 'd':
42 checkdepth = 0;
43 break;
44 case 'h':
45 frompicz = 0;
46 break;
47 case 'p':
48 frompicz = 1;
49 break;
50 default:
51 goto userr;
52 }
53 if (i >= argc || frompicz < 0)
54 goto userr;
55 if (frompicz && (argc-i)%2)
56 goto userr;
57 noutsects = openholo(argv[1], 1);
58 if (frompicz) {
59 for ( ; i < argc; i += 2)
60 addpicz(argv[i], argv[i+1]);
61 } else {
62 if (BKBSIZE*1024*1.5 > hdcachesize)
63 hdcachesize = BKBSIZE*1024*1.5;
64 for ( ; i < argc; i++)
65 addholo(argv[i]);
66 }
67 quit(0);
68 userr:
69 fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n",
70 progname);
71 fprintf(stderr, " Or: %s output.hdk [-u][-d] -p inp1.pic inp1.zbf ..\n",
72 progname);
73 exit(1);
74 }
75
76
77 #define H_BADF 01
78 #define H_OBST 02
79 #define H_OBSF 04
80
81 int
82 holheadline(s, hf) /* check holodeck header line */
83 register char *s;
84 int *hf;
85 {
86 char fmt[32];
87
88 if (formatval(fmt, s)) {
89 if (strcmp(fmt, HOLOFMT))
90 *hf |= H_BADF;
91 else
92 *hf &= ~H_BADF;
93 return(0);
94 }
95 if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
96 s += 13;
97 while (*s == ' ') s++;
98 if (*s == 't' | *s == 'T')
99 *hf |= H_OBST;
100 else if (*s == 'f' | *s == 'F')
101 *hf |= H_OBSF;
102 else
103 error(WARNING, "bad OBSTRUCTIONS value in holodeck");
104 return(0);
105 }
106 return(0);
107 }
108
109 int
110 openholo(fname, append) /* open existing holodeck file for i/o */
111 char *fname;
112 int append;
113 {
114 extern long ftell();
115 FILE *fp;
116 int fd;
117 int hflags = 0;
118 long nextloc;
119 int n;
120 /* open holodeck file */
121 if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) {
122 sprintf(errmsg, "cannot open \"%s\" for %s", fname,
123 append ? "appending" : "reading");
124 error(SYSTEM, errmsg);
125 }
126 /* check header and magic number */
127 if (getheader(fp, holheadline, &hflags) < 0 ||
128 hflags&H_BADF || getw(fp) != HOLOMAGIC) {
129 sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
130 error(USER, errmsg);
131 }
132 fd = dup(fileno(fp)); /* dup file handle */
133 nextloc = ftell(fp); /* get stdio position */
134 fclose(fp); /* done with stdio */
135 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
136 lseek(fd, nextloc, 0);
137 read(fd, (char *)&nextloc, sizeof(nextloc));
138 hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
139 hflags&H_OBSF ? &unobstr : (char *)NULL;
140 }
141 return(n);
142 }
143
144 #undef H_BADF
145 #undef H_OBST
146 #undef H_OBSF
147
148
149 addray(ro, rd, d, cv) /* add a ray to our output holodeck */
150 FVECT ro, rd;
151 double d;
152 COLR cv;
153 {
154 int sn, bi, n;
155 register HOLO *hp;
156 GCOORD gc[2];
157 BYTE rr[2][2];
158 BEAM *bp;
159 double d0, d1;
160 unsigned dc;
161 register RAYVAL *rv;
162 /* check each output section */
163 for (sn = noutsects; sn--; ) {
164 hp = hdlist[sn];
165 d0 = hdinter(gc, rr, &d1, hp, ro, rd);
166 if (d <= d0 || d1 < -0.001)
167 continue; /* missed section */
168 if (checkdepth) { /* check depth */
169 if (hp->priv == &obstr && d0 < -0.001)
170 continue; /* ray starts too late */
171 if (hp->priv == &unobstr && d < 0.999*d1)
172 continue; /* ray ends too soon */
173 }
174 dc = hdcode(hp, d-d0);
175 bi = hdbindex(hp, gc); /* check for duplicates */
176 if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) {
177 for (n = bp->nrm, rv = hdbray(bp); n--; rv++)
178 if (rv->d == dc &&
179 rv->r[0][0] == rr[0][0] &&
180 rv->r[0][1] == rr[0][1] &&
181 rv->r[1][0] == rr[1][0] &&
182 rv->r[1][1] == rr[1][1])
183 break;
184 if (n >= 0)
185 continue; /* found a matching ray */
186 }
187 rv = hdnewrays(hp, bi, 1);
188 rv->d = dc;
189 rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
190 rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
191 copycolr(rv->v, cv);
192 }
193 }
194
195
196 static BEAMI *beamdir;
197
198 static int
199 bpcmp(b1p, b2p) /* compare beam positions on disk */
200 int *b1p, *b2p;
201 {
202 register long pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
203
204 if (pdif > 0L) return(1);
205 if (pdif < 0L) return(-1);
206 return(0);
207 }
208
209 static int
210 addclump(hp, bq, nb) /* transfer the given clump and free */
211 HOLO *hp;
212 int *bq, nb;
213 {
214 GCOORD gc[2];
215 FVECT ro, rd;
216 double d;
217 int i;
218 register int k;
219 register BEAM *bp;
220 /* sort based on file position */
221 beamdir = hp->bi;
222 qsort((char *)bq, nb, sizeof(*bq), bpcmp);
223 /* transfer each beam */
224 for (i = 0; i < nb; i++) {
225 bp = hdgetbeam(hp, bq[i]);
226 hdbcoord(gc, hp, bq[i]);
227 /* add each ray to output */
228 for (k = bp->nrm; k--; ) {
229 d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
230 if (hp->priv == &unobstr)
231 VSUM(ro, ro, rd, d);
232 else
233 d = 0.;
234 d = hddepth(hp, hdbray(bp)[k].d) - d;
235 addray(ro, rd, d, hdbray(bp)[k].v);
236 }
237 hdfreebeam(hp, bq[i]); /* free the beam */
238 }
239 hdflush(NULL); /* write & free clump */
240 return(0);
241 }
242
243 addholo(hdf) /* add a holodeck file */
244 char *hdf;
245 {
246 int fd;
247 /* open the holodeck for reading */
248 openholo(hdf, 0);
249 fd = hdlist[noutsects]->fd; /* remember the file handle */
250 while (hdlist[noutsects] != NULL) { /* load each section */
251 /* clump the beams */
252 clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
253 hddone(hdlist[noutsects]); /* free the section */
254 }
255 close(fd); /* close input file */
256 }
257
258
259 struct phead {
260 VIEW vw;
261 double expos;
262 short gotview;
263 short badfmt;
264 short altprims;
265 };
266
267
268 int
269 picheadline(s, ph) /* process picture header line */
270 char *s;
271 struct phead *ph;
272 {
273 char fmt[32];
274
275 if (formatval(fmt, s)) {
276 ph->badfmt = strcmp(fmt, COLRFMT);
277 return(0);
278 }
279 if (isprims(s)) {
280 ph->altprims++; /* don't want to deal with this */
281 return(0);
282 }
283 if (isexpos(s)) {
284 ph->expos *= exposval(s);
285 return(0);
286 }
287 if (isview(s)) {
288 ph->gotview += sscanview(&ph->vw, s);
289 return(0);
290 }
291 return(0);
292 }
293
294
295 addpicz(pcf, zbf) /* add a picture + depth-buffer */
296 char *pcf, *zbf;
297 {
298 FILE *pfp;
299 int zfd;
300 COLR *cscn;
301 float *zscn;
302 struct phead phd;
303 int eshft;
304 double emult;
305 RESOLU prs;
306 FLOAT vl[2];
307 FVECT ro, rd;
308 double aftd;
309 COLOR ctmp;
310 int j;
311 register int i;
312 /* open files */
313 if ((pfp = fopen(pcf, "r")) == NULL) {
314 sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
315 error(SYSTEM, pcf);
316 }
317 if ((zfd = open(zbf, O_RDONLY)) < 0) {
318 sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
319 error(SYSTEM, pcf);
320 }
321 /* load picture header */
322 copystruct(&phd.vw, &stdview);
323 phd.expos = 1.0;
324 phd.badfmt = phd.gotview = phd.altprims = 0;
325 if (getheader(pfp, picheadline, &phd) < 0 ||
326 phd.badfmt || !fgetsresolu(&prs, pfp)) {
327 sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
328 error(USER, errmsg);
329 }
330 if (!phd.gotview || setview(&phd.vw) != NULL) {
331 sprintf(errmsg, "missing/illegal view in picture \"%s\"",
332 pcf);
333 error(USER, errmsg);
334 }
335 if (phd.altprims) {
336 sprintf(errmsg, "ignoring primary values in picture \"%s\"",
337 pcf);
338 error(WARNING, errmsg);
339 }
340 /* figure out what to do about exposure */
341 if (phd.expos < 0.99 | phd.expos > 1.01) {
342 emult = -log(phd.expos)/log(2.);
343 eshft = emult >= 0. ? emult+.5 : emult-.5;
344 emult -= (double)eshft;
345 if (emult <= 0.01 & emult >= -0.01)
346 emult = -1.;
347 else {
348 emult = 1./phd.expos;
349 eshft = 0;
350 }
351 } else {
352 emult = -1.;
353 eshft = 0;
354 }
355 /* allocate buffers */
356 cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
357 zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
358 if (cscn == NULL | zscn == NULL)
359 error(SYSTEM, "out of memory in addpicz");
360 /* read and process each scanline */
361 for (j = 0; j < numscans(&prs); j++) {
362 i = scanlen(&prs); /* read colrs */
363 if (freadcolrs(cscn, i, pfp) < 0) {
364 sprintf(errmsg, "error reading picture \"%s\"", pcf);
365 error(USER, errmsg);
366 }
367 if (eshft) /* shift exposure */
368 shiftcolrs(cscn, i, eshft);
369 i *= sizeof(float); /* read depth */
370 if (read(zfd, (char *)zscn, i) != i) {
371 sprintf(errmsg, "error reading depth file \"%s\"", zbf);
372 error(USER, errmsg);
373 }
374 for (i = scanlen(&prs); i--; ) { /* do each pixel */
375 pix2loc(vl, &prs, i, j);
376 aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
377 if (aftd < -FTINY)
378 continue; /* off view */
379 if (aftd > FTINY && zscn[i] > aftd)
380 continue; /* aft clipped */
381 if (emult > 0.) { /* whatta pain */
382 colr_color(ctmp, cscn[i]);
383 scalecolor(ctmp, emult);
384 setcolr(cscn[i], colval(ctmp,RED),
385 colval(ctmp,GRN), colval(ctmp,BLU));
386 }
387 addray(ro, rd, (double)zscn[i], cscn[i]);
388 }
389 }
390 /* write output and free beams */
391 hdflush(NULL);
392 /* clean up */
393 free((char *)cscn);
394 free((char *)zscn);
395 fclose(pfp);
396 close(zfd);
397 }
398
399
400 eputs(s) /* put error message to stderr */
401 register char *s;
402 {
403 static int midline = 0;
404
405 if (!*s)
406 return;
407 if (!midline++) { /* prepend line with program name */
408 fputs(progname, stderr);
409 fputs(": ", stderr);
410 }
411 fputs(s, stderr);
412 if (s[strlen(s)-1] == '\n') {
413 fflush(stderr);
414 midline = 0;
415 }
416 }
417
418
419 quit(code) /* exit the program gracefully */
420 int code;
421 {
422 hdsync(NULL, 1); /* write out any buffered data */
423 exit(code);
424 }