1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Copy data into a holodeck file |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "platform.h" |
9 |
+ |
#include "rterror.h" |
10 |
|
#include "holo.h" |
11 |
|
#include "view.h" |
13 |
– |
#include "resolu.h" |
12 |
|
|
13 |
< |
#ifndef BLOADSIZE |
14 |
< |
#define BLOADSIZE 1024 /* number of input beams to load at a time */ |
13 |
> |
#ifndef BKBSIZE |
14 |
> |
#define BKBSIZE 256 /* beam clump size (kilobytes) */ |
15 |
|
#endif |
16 |
|
|
17 |
< |
int checkdepth = 1; /* check depth (!-f option)? */ |
18 |
< |
int checkrepeats = 0; /* check for repeats (-c option)? */ |
17 |
> |
int checkdepth = 1; /* check depth (!-d option)? */ |
18 |
> |
int checkrepeats = 0; /* check for repeats (-u option)? */ |
19 |
|
int frompicz; /* input from pictures & depth-buffers? */ |
20 |
|
int noutsects; /* number of output sections */ |
21 |
|
char obstr, unobstr; /* flag pointer values */ |
22 |
|
|
23 |
|
char *progname; /* global argv[0] */ |
24 |
|
|
25 |
+ |
struct phead { |
26 |
+ |
VIEW vw; |
27 |
+ |
double expos; |
28 |
+ |
short gotview; |
29 |
+ |
short badfmt; |
30 |
+ |
short altprims; |
31 |
+ |
}; |
32 |
+ |
static int openholo(char *fname, int append); |
33 |
+ |
static void addray(FVECT ro, FVECT rd, double d, COLR cv); |
34 |
+ |
static gethfunc holheadline; |
35 |
+ |
static int bpcmp(const void *b1p, const void *b2p); |
36 |
+ |
static int addclump(HOLO *hp, int *bq, int nb); |
37 |
+ |
static void addholo(char *hdf); |
38 |
+ |
static gethfunc picheadline; |
39 |
+ |
static void addpicz(char *pcf, char *zbf); |
40 |
|
|
41 |
< |
main(argc, argv) |
42 |
< |
int argc; |
43 |
< |
char *argv[]; |
41 |
> |
|
42 |
> |
|
43 |
> |
int |
44 |
> |
main( |
45 |
> |
int argc, |
46 |
> |
char *argv[] |
47 |
> |
) |
48 |
|
{ |
49 |
|
int i; |
50 |
|
|
52 |
|
frompicz = -1; |
53 |
|
for (i = 2; i < argc && argv[i][0] == '-'; i++) |
54 |
|
switch (argv[i][1]) { |
55 |
< |
case 'c': |
55 |
> |
case 'u': |
56 |
|
checkrepeats = 1; |
57 |
|
break; |
58 |
< |
case 'f': |
58 |
> |
case 'd': |
59 |
|
checkdepth = 0; |
60 |
|
break; |
61 |
|
case 'h': |
72 |
|
if (frompicz && (argc-i)%2) |
73 |
|
goto userr; |
74 |
|
noutsects = openholo(argv[1], 1); |
75 |
< |
if (frompicz) |
75 |
> |
if (frompicz) { |
76 |
|
for ( ; i < argc; i += 2) |
77 |
|
addpicz(argv[i], argv[i+1]); |
78 |
< |
else |
78 |
> |
} else { |
79 |
> |
if (BKBSIZE*1024*1.5 > hdcachesize) |
80 |
> |
hdcachesize = BKBSIZE*1024*1.5; |
81 |
|
for ( ; i < argc; i++) |
82 |
|
addholo(argv[i]); |
83 |
+ |
} |
84 |
|
quit(0); |
85 |
|
userr: |
86 |
< |
fprintf(stderr, "Usage: %s output.hdk [-c][-f] -h inp1.hdk ..\n", |
86 |
> |
fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n", |
87 |
|
progname); |
88 |
< |
fprintf(stderr, " Or: %s output.hdk [-c][-f] -pz inp1.pic inp1.zbf ..\n", |
88 |
> |
fprintf(stderr, " Or: %s output.hdk [-u][-d] -p inp1.hdr inp1.zbf ..\n", |
89 |
|
progname); |
90 |
|
exit(1); |
91 |
|
} |
95 |
|
#define H_OBST 02 |
96 |
|
#define H_OBSF 04 |
97 |
|
|
98 |
< |
holheadline(s, hf) /* check holodeck header line */ |
99 |
< |
register char *s; |
100 |
< |
int *hf; |
98 |
> |
static int |
99 |
> |
holheadline( /* check holodeck header line */ |
100 |
> |
char *s, |
101 |
> |
void *vhf |
102 |
> |
) |
103 |
|
{ |
104 |
< |
char fmt[32]; |
104 |
> |
char fmt[MAXFMTLEN]; |
105 |
> |
int *hf = vhf; |
106 |
|
|
107 |
|
if (formatval(fmt, s)) { |
108 |
|
if (strcmp(fmt, HOLOFMT)) |
109 |
|
*hf |= H_BADF; |
110 |
|
else |
111 |
|
*hf &= ~H_BADF; |
112 |
< |
return; |
112 |
> |
return(0); |
113 |
|
} |
114 |
|
if (!strncmp(s, "OBSTRUCTIONS=", 13)) { |
115 |
|
s += 13; |
116 |
|
while (*s == ' ') s++; |
117 |
< |
if (*s == 't' | *s == 'T') |
117 |
> |
if ((*s == 't') | (*s == 'T')) |
118 |
|
*hf |= H_OBST; |
119 |
< |
else if (*s == 'f' | *s == 'F') |
119 |
> |
else if ((*s == 'f') | (*s == 'F')) |
120 |
|
*hf |= H_OBSF; |
121 |
|
else |
122 |
|
error(WARNING, "bad OBSTRUCTIONS value in holodeck"); |
123 |
< |
return; |
123 |
> |
return(0); |
124 |
|
} |
125 |
+ |
return(0); |
126 |
|
} |
127 |
|
|
128 |
|
int |
129 |
< |
openholo(fname, append) /* open existing holodeck file for i/o */ |
130 |
< |
char *fname; |
131 |
< |
int append; |
129 |
> |
openholo( /* open existing holodeck file for i/o */ |
130 |
> |
char *fname, |
131 |
> |
int append |
132 |
> |
) |
133 |
|
{ |
109 |
– |
extern long ftell(); |
134 |
|
FILE *fp; |
135 |
|
int fd; |
136 |
|
int hflags = 0; |
137 |
< |
long nextloc; |
137 |
> |
off_t nextloc; |
138 |
|
int n; |
139 |
|
/* open holodeck file */ |
140 |
< |
if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) { |
140 |
> |
if ((fp = fopen(fname, append ? "rb+" : "rb")) == NULL) { |
141 |
|
sprintf(errmsg, "cannot open \"%s\" for %s", fname, |
142 |
|
append ? "appending" : "reading"); |
143 |
|
error(SYSTEM, errmsg); |
152 |
|
nextloc = ftell(fp); /* get stdio position */ |
153 |
|
fclose(fp); /* done with stdio */ |
154 |
|
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
155 |
< |
lseek(fd, nextloc, 0); |
155 |
> |
lseek(fd, nextloc, SEEK_SET); |
156 |
|
read(fd, (char *)&nextloc, sizeof(nextloc)); |
157 |
|
hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr : |
158 |
|
hflags&H_OBSF ? &unobstr : (char *)NULL; |
165 |
|
#undef H_OBSF |
166 |
|
|
167 |
|
|
168 |
< |
addray(ro, rd, d, cv) /* add a ray to our output holodeck */ |
169 |
< |
FVECT ro, rd; |
170 |
< |
double d; |
171 |
< |
COLR cv; |
168 |
> |
void |
169 |
> |
addray( /* add a ray to our output holodeck */ |
170 |
> |
FVECT ro, |
171 |
> |
FVECT rd, |
172 |
> |
double d, |
173 |
> |
COLR cv |
174 |
> |
) |
175 |
|
{ |
176 |
|
int sn, bi, n; |
177 |
< |
register HOLO *hp; |
177 |
> |
HOLO *hp; |
178 |
|
GCOORD gc[2]; |
179 |
< |
BYTE rr[2][2]; |
179 |
> |
uby8 rr[2][2]; |
180 |
|
BEAM *bp; |
181 |
|
double d0, d1; |
182 |
|
unsigned dc; |
183 |
< |
register RAYVAL *rv; |
183 |
> |
RAYVAL *rv; |
184 |
|
/* check each output section */ |
185 |
|
for (sn = noutsects; sn--; ) { |
186 |
|
hp = hdlist[sn]; |
197 |
|
bi = hdbindex(hp, gc); /* check for duplicates */ |
198 |
|
if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) { |
199 |
|
for (n = bp->nrm, rv = hdbray(bp); n--; rv++) |
200 |
< |
if (rv->d == dc && |
200 |
> |
if ((hp->priv != NULL || rv->d == dc) && |
201 |
|
rv->r[0][0] == rr[0][0] && |
202 |
|
rv->r[0][1] == rr[0][1] && |
203 |
|
rv->r[1][0] == rr[1][0] && |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
< |
addholo(hdf) /* add a holodeck file */ |
219 |
< |
char *hdf; |
218 |
> |
static BEAMI *beamdir; |
219 |
> |
|
220 |
> |
static int |
221 |
> |
bpcmp( /* compare beam positions on disk */ |
222 |
> |
const void *b1p, |
223 |
> |
const void *b2p |
224 |
> |
) |
225 |
|
{ |
226 |
< |
HDBEAMI hbl[BLOADSIZE]; |
227 |
< |
int fd; |
228 |
< |
register HOLO *hp; |
229 |
< |
register BEAM *bp; |
226 |
> |
off_t pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo; |
227 |
> |
|
228 |
> |
if (pdif > 0L) return(1); |
229 |
> |
if (pdif < 0L) return(-1); |
230 |
> |
return(0); |
231 |
> |
} |
232 |
> |
|
233 |
> |
static int |
234 |
> |
addclump( /* transfer the given clump and free */ |
235 |
> |
HOLO *hp, |
236 |
> |
int *bq, |
237 |
> |
int nb |
238 |
> |
) |
239 |
> |
{ |
240 |
|
GCOORD gc[2]; |
241 |
|
FVECT ro, rd; |
242 |
|
double d; |
243 |
< |
int i, j, n, li; |
244 |
< |
register int k; |
243 |
> |
int i; |
244 |
> |
int k; |
245 |
> |
BEAM *bp; |
246 |
> |
/* sort based on file position */ |
247 |
> |
beamdir = hp->bi; |
248 |
> |
qsort((char *)bq, nb, sizeof(*bq), bpcmp); |
249 |
> |
/* transfer each beam */ |
250 |
> |
for (i = 0; i < nb; i++) { |
251 |
> |
bp = hdgetbeam(hp, bq[i]); |
252 |
> |
hdbcoord(gc, hp, bq[i]); |
253 |
> |
/* add each ray to output */ |
254 |
> |
for (k = bp->nrm; k--; ) { |
255 |
> |
d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r); |
256 |
> |
if (hp->priv == &unobstr) |
257 |
> |
VSUM(ro, ro, rd, d); |
258 |
> |
else |
259 |
> |
d = 0.; |
260 |
> |
d = hddepth(hp, hdbray(bp)[k].d) - d; |
261 |
> |
addray(ro, rd, d, hdbray(bp)[k].v); |
262 |
> |
} |
263 |
> |
hdfreebeam(hp, bq[i]); /* free the beam */ |
264 |
> |
} |
265 |
> |
return(0); |
266 |
> |
} |
267 |
> |
|
268 |
> |
|
269 |
> |
void |
270 |
> |
addholo( /* add a holodeck file */ |
271 |
> |
char *hdf |
272 |
> |
) |
273 |
> |
{ |
274 |
> |
int fd; |
275 |
|
/* open the holodeck for reading */ |
276 |
|
openholo(hdf, 0); |
277 |
|
fd = hdlist[noutsects]->fd; /* remember the file handle */ |
278 |
< |
while ((hp = hdlist[noutsects]) != NULL) { /* load each section */ |
279 |
< |
for (j = 0; j < nbeams(hp); j++) { /* load each beam */ |
280 |
< |
if (!(li = j % BLOADSIZE)) { /* optimize order */ |
281 |
< |
if (j+BLOADSIZE > nbeams(hp)) |
210 |
< |
k = n = nbeams(hp) - j; |
211 |
< |
else |
212 |
< |
k = n = BLOADSIZE; |
213 |
< |
while (k--) { |
214 |
< |
hbl[k].h = hp; |
215 |
< |
hbl[k].b = j+k+1; |
216 |
< |
} |
217 |
< |
qsort((char *)hbl, n, |
218 |
< |
sizeof(HDBEAMI), hdfilord); |
219 |
< |
} |
220 |
< |
if ((bp = hdgetbeam(hp, hbl[li].b)) != NULL) { |
221 |
< |
hdbcoord(gc, hp, hbl[li].b); |
222 |
< |
for (k = bp->nrm; k--; ) { |
223 |
< |
d = hdray(ro, rd, |
224 |
< |
hp, gc, hdbray(bp)[k].r); |
225 |
< |
if (hp->priv == &unobstr) |
226 |
< |
VSUM(ro, ro, rd, d); |
227 |
< |
else |
228 |
< |
d = 0.; |
229 |
< |
d = hddepth(hp, hdbray(bp)[k].d) - d; |
230 |
< |
addray(ro, rd, d, hdbray(bp)[k].v); |
231 |
< |
} |
232 |
< |
hdfreebeam(hp, hbl[li].b); /* free beam */ |
233 |
< |
} |
234 |
< |
} |
235 |
< |
hddone(hp); /* free the section */ |
278 |
> |
while (hdlist[noutsects] != NULL) { /* load each section */ |
279 |
> |
/* clump the beams */ |
280 |
> |
clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump); |
281 |
> |
hddone(hdlist[noutsects]); /* free the section */ |
282 |
|
} |
283 |
< |
close(fd); /* close the file */ |
283 |
> |
close(fd); /* close input file */ |
284 |
> |
hdflush(NULL); /* flush output */ |
285 |
|
} |
286 |
|
|
287 |
|
|
241 |
– |
struct phead { |
242 |
– |
VIEW vw; |
243 |
– |
double expos; |
244 |
– |
short gotview; |
245 |
– |
short badfmt; |
246 |
– |
short altprims; |
247 |
– |
}; |
288 |
|
|
289 |
< |
|
290 |
< |
picheadline(s, ph) /* process picture header line */ |
291 |
< |
char *s; |
292 |
< |
struct phead *ph; |
289 |
> |
static int |
290 |
> |
picheadline( /* process picture header line */ |
291 |
> |
char *s, |
292 |
> |
void *vph |
293 |
> |
) |
294 |
|
{ |
295 |
|
char fmt[32]; |
296 |
+ |
struct phead *ph = vph; |
297 |
|
|
298 |
|
if (formatval(fmt, s)) { |
299 |
|
ph->badfmt = strcmp(fmt, COLRFMT); |
300 |
< |
return; |
300 |
> |
return(0); |
301 |
|
} |
302 |
|
if (isprims(s)) { |
303 |
|
ph->altprims++; /* don't want to deal with this */ |
304 |
< |
return; |
304 |
> |
return(0); |
305 |
|
} |
306 |
|
if (isexpos(s)) { |
307 |
|
ph->expos *= exposval(s); |
308 |
< |
return; |
308 |
> |
return(0); |
309 |
|
} |
310 |
|
if (isview(s)) { |
311 |
|
ph->gotview += sscanview(&ph->vw, s); |
312 |
< |
return; |
312 |
> |
return(0); |
313 |
|
} |
314 |
+ |
return(0); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
< |
addpicz(pcf, zbf) /* add a picture + depth-buffer */ |
319 |
< |
char *pcf, *zbf; |
318 |
> |
void |
319 |
> |
addpicz( /* add a picture + depth-buffer */ |
320 |
> |
char *pcf, |
321 |
> |
char *zbf |
322 |
> |
) |
323 |
|
{ |
324 |
|
FILE *pfp; |
325 |
|
int zfd; |
329 |
|
int eshft; |
330 |
|
double emult; |
331 |
|
RESOLU prs; |
332 |
< |
FLOAT vl[2]; |
332 |
> |
RREAL vl[2]; |
333 |
|
FVECT ro, rd; |
334 |
|
double aftd; |
335 |
|
COLOR ctmp; |
336 |
|
int j; |
337 |
< |
register int i; |
337 |
> |
int i; |
338 |
|
/* open files */ |
339 |
< |
if ((pfp = fopen(pcf, "r")) == NULL) { |
339 |
> |
if ((pfp = fopen(pcf, "rb")) == NULL) { |
340 |
|
sprintf(errmsg, "cannot open picture file \"%s\"", pcf); |
341 |
|
error(SYSTEM, pcf); |
342 |
|
} |
344 |
|
sprintf(errmsg, "cannot open depth file \"%s\"", zbf); |
345 |
|
error(SYSTEM, pcf); |
346 |
|
} |
347 |
+ |
SET_FD_BINARY(zfd); |
348 |
|
/* load picture header */ |
349 |
< |
copystruct(&phd.vw, &stdview); |
349 |
> |
phd.vw = stdview; |
350 |
|
phd.expos = 1.0; |
351 |
|
phd.badfmt = phd.gotview = phd.altprims = 0; |
352 |
|
if (getheader(pfp, picheadline, &phd) < 0 || |
360 |
|
error(USER, errmsg); |
361 |
|
} |
362 |
|
if (phd.altprims) { |
363 |
< |
sprintf(errmsg, "ignoring primary values in picture \"%s\"", |
363 |
> |
sprintf(errmsg, "ignoring color primaries in picture \"%s\"", |
364 |
|
pcf); |
365 |
|
error(WARNING, errmsg); |
366 |
|
} |
367 |
|
/* figure out what to do about exposure */ |
368 |
< |
if (phd.expos < 0.99 | phd.expos > 1.01) { |
368 |
> |
if ((phd.expos < 0.99) | (phd.expos > 1.01)) { |
369 |
|
emult = -log(phd.expos)/log(2.); |
370 |
|
eshft = emult >= 0. ? emult+.5 : emult-.5; |
371 |
|
emult -= (double)eshft; |
372 |
< |
if (emult <= 0.01 & emult >= -0.01) |
372 |
> |
if ((emult <= 0.01) & (emult >= -0.01)) |
373 |
|
emult = -1.; |
374 |
|
else { |
375 |
|
emult = 1./phd.expos; |
382 |
|
/* allocate buffers */ |
383 |
|
cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR)); |
384 |
|
zscn = (float *)malloc(scanlen(&prs)*sizeof(float)); |
385 |
< |
if (cscn == NULL | zscn == NULL) |
385 |
> |
if ((cscn == NULL) | (zscn == NULL)) |
386 |
|
error(SYSTEM, "out of memory in addpicz"); |
387 |
|
/* read and process each scanline */ |
388 |
|
for (j = 0; j < numscans(&prs); j++) { |
393 |
|
} |
394 |
|
if (eshft) /* shift exposure */ |
395 |
|
shiftcolrs(cscn, i, eshft); |
396 |
< |
i *= sizeof(float); /* read depth */ |
397 |
< |
if (read(zfd, (char *)zscn, i) != i) { |
396 |
> |
/* read depth */ |
397 |
> |
if (read(zfd, zscn, i*sizeof(float)) != i*sizeof(float)) { |
398 |
|
sprintf(errmsg, "error reading depth file \"%s\"", zbf); |
399 |
|
error(USER, errmsg); |
400 |
|
} |
401 |
< |
for (i = scanlen(&prs); i--; ) { /* do each pixel */ |
401 |
> |
while (i--) { /* process each pixel */ |
402 |
> |
if (zscn[i] <= 0.0) |
403 |
> |
continue; /* illegal depth */ |
404 |
|
pix2loc(vl, &prs, i, j); |
405 |
|
aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]); |
406 |
|
if (aftd < -FTINY) |
416 |
|
addray(ro, rd, (double)zscn[i], cscn[i]); |
417 |
|
} |
418 |
|
} |
419 |
+ |
/* write output and free beams */ |
420 |
+ |
hdflush(NULL); |
421 |
|
/* clean up */ |
422 |
< |
free((char *)cscn); |
423 |
< |
free((char *)zscn); |
422 |
> |
free((void *)cscn); |
423 |
> |
free((void *)zscn); |
424 |
|
fclose(pfp); |
425 |
|
close(zfd); |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
< |
eputs(s) /* put error message to stderr */ |
430 |
< |
register char *s; |
429 |
> |
void |
430 |
> |
eputs( /* put error message to stderr */ |
431 |
> |
char *s |
432 |
> |
) |
433 |
|
{ |
434 |
|
static int midline = 0; |
435 |
|
|
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
< |
quit(code) /* exit the program gracefully */ |
451 |
< |
int code; |
450 |
> |
void |
451 |
> |
quit( /* exit the program gracefully */ |
452 |
> |
int code |
453 |
> |
) |
454 |
|
{ |
455 |
|
hdsync(NULL, 1); /* write out any buffered data */ |
456 |
|
exit(code); |