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