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 |
int frompicz; /* input from pictures & depth-buffers? */ |
16 |
int noutsects; /* number of output sections */ |
17 |
int obstructions = -1; /* interior obstructions allowed? */ |
18 |
|
19 |
char *progname; /* global argv[0] */ |
20 |
|
21 |
|
22 |
main(argc, argv) |
23 |
int argc; |
24 |
char *argv[]; |
25 |
{ |
26 |
int i; |
27 |
|
28 |
progname = argv[0]; |
29 |
if (argc < 4) |
30 |
goto userr; |
31 |
if (!strcmp(argv[2], "-h")) |
32 |
frompicz = 0; |
33 |
else if (!strcmp(argv[2], "-pz")) |
34 |
frompicz = 1; |
35 |
else |
36 |
goto userr; |
37 |
if (frompicz && (argc-3)%2) |
38 |
goto userr; |
39 |
noutsects = openholo(argv[1], 1); |
40 |
if (frompicz) |
41 |
for (i = 3; i < argc; i += 2) |
42 |
addpicz(argv[i], argv[i+1]); |
43 |
else |
44 |
for (i = 3; i < argc; i++) |
45 |
addholo(argv[i]); |
46 |
quit(0); |
47 |
userr: |
48 |
fprintf(stderr, "Usage: %s output.hdk -h inp1.hdk ..\n", progname); |
49 |
fprintf(stderr, " Or: %s output.hdk -pz inp1.pic inp1.zbf ..\n", |
50 |
progname); |
51 |
exit(1); |
52 |
} |
53 |
|
54 |
|
55 |
holheadline(s, bf) /* check holodeck header line */ |
56 |
register char *s; |
57 |
int *bf; |
58 |
{ |
59 |
char fmt[32]; |
60 |
|
61 |
if (formatval(fmt, s)) { |
62 |
*bf = strcmp(fmt, HOLOFMT); |
63 |
return; |
64 |
} |
65 |
if (!strncmp(s, "OBSTRUCTIONS=", 13)) { |
66 |
s += 13; |
67 |
while (*s == ' ') s++; |
68 |
if (*s == 't' | *s == 'T') |
69 |
obstructions = 1; |
70 |
else if (*s == 'f' | *s == 'F') |
71 |
obstructions = 0; |
72 |
else |
73 |
error(WARNING, "bad OBSTRUCTIONS value in holodeck"); |
74 |
return; |
75 |
} |
76 |
} |
77 |
|
78 |
|
79 |
int |
80 |
openholo(fname, append) /* open existing holodeck file for i/o */ |
81 |
char *fname; |
82 |
int append; |
83 |
{ |
84 |
extern long ftell(); |
85 |
FILE *fp; |
86 |
int fd; |
87 |
int badfmt = 0; |
88 |
int4 nextloc; |
89 |
int n; |
90 |
/* open holodeck file */ |
91 |
if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) { |
92 |
sprintf(errmsg, "cannot open \"%s\" for %s", fname, |
93 |
append ? "appending" : "reading"); |
94 |
error(SYSTEM, errmsg); |
95 |
} |
96 |
/* check header and magic number */ |
97 |
if (append) |
98 |
badfmt |= getheader(fp, holheadline, &badfmt) < 0; |
99 |
else |
100 |
badfmt = checkheader(fp, HOLOFMT, NULL) < 0; |
101 |
if (badfmt || getw(fp) != HOLOMAGIC) { |
102 |
sprintf(errmsg, "file \"%s\" not in holodeck format", fname); |
103 |
error(USER, errmsg); |
104 |
} |
105 |
fd = dup(fileno(fp)); /* dup file handle */ |
106 |
nextloc = ftell(fp); /* get stdio position */ |
107 |
fclose(fp); /* done with stdio */ |
108 |
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
109 |
lseek(fd, (long)nextloc, 0); |
110 |
read(fd, (char *)&nextloc, sizeof(nextloc)); |
111 |
hdinit(fd, NULL); |
112 |
} |
113 |
return(n); |
114 |
} |
115 |
|
116 |
|
117 |
addray(ro, rd, d, cv) /* add a ray to our output holodeck */ |
118 |
FVECT ro, rd; |
119 |
double d; |
120 |
COLR cv; |
121 |
{ |
122 |
int sn; |
123 |
register HOLO *hp; |
124 |
GCOORD gc[2]; |
125 |
BYTE rr[2][2]; |
126 |
double d0, d1; |
127 |
register RAYVAL *rv; |
128 |
/* check each output section */ |
129 |
for (sn = noutsects; sn--; ) { |
130 |
hp = hdlist[sn]; |
131 |
d0 = hdinter(gc, rr, &d1, hp, ro, rd); |
132 |
if (d <= d0 || d1 < -0.001) |
133 |
continue; /* missed section */ |
134 |
if (obstructions > 0 && d0 < -0.001) |
135 |
continue; /* ray starts too late */ |
136 |
if (!obstructions && d < 0.999*d1) |
137 |
continue; /* ray ends too soon */ |
138 |
/* should we check for duplicates? */ |
139 |
rv = hdnewrays(hp, hdbindex(hp, gc), 1); |
140 |
rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1]; |
141 |
rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1]; |
142 |
copycolr(rv->v, cv); |
143 |
rv->d = hdcode(hp, d-d0); |
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
addholo(hdf) /* add a holodeck file */ |
149 |
char *hdf; |
150 |
{ |
151 |
int fd; |
152 |
register HOLO *hp; |
153 |
register BEAM *bp; |
154 |
GCOORD gc[2]; |
155 |
FVECT ro, rd; |
156 |
double d; |
157 |
int i, j; |
158 |
register int k; |
159 |
|
160 |
openholo(hdf, 0); /* open the holodeck for reading */ |
161 |
fd = hdlist[noutsects]->fd; /* remember the file handle */ |
162 |
while ((hp = hdlist[noutsects]) != NULL) { /* load each section */ |
163 |
for (j = nbeams(hp); j > 0; j--) /* load each beam */ |
164 |
if ((bp = hdgetbeam(hp, j)) != NULL) { |
165 |
hdbcoord(gc, hp, j); |
166 |
for (k = bp->nrm; k--; ) { |
167 |
hdray(ro, rd, hp, gc, hdbray(bp)[k].r); |
168 |
d = hddepth(hp, hdbray(bp)[k].d); |
169 |
addray(ro, rd, d, hdbray(bp)[k].v); |
170 |
} |
171 |
hdfreebeam(hp, j); /* free the beam */ |
172 |
} |
173 |
hddone(hp); /* free the section */ |
174 |
} |
175 |
close(fd); /* close the file */ |
176 |
} |
177 |
|
178 |
|
179 |
struct phead { |
180 |
VIEW vw; |
181 |
double expos; |
182 |
short gotview; |
183 |
short badfmt; |
184 |
short altprims; |
185 |
}; |
186 |
|
187 |
|
188 |
picheadline(s, ph) /* process picture header line */ |
189 |
char *s; |
190 |
struct phead *ph; |
191 |
{ |
192 |
char fmt[32]; |
193 |
|
194 |
if (formatval(fmt, s)) { |
195 |
ph->badfmt = strcmp(fmt, COLRFMT); |
196 |
return; |
197 |
} |
198 |
if (isprims(s)) { |
199 |
ph->altprims++; /* don't want to deal with this */ |
200 |
return; |
201 |
} |
202 |
if (isexpos(s)) { |
203 |
ph->expos *= exposval(s); |
204 |
return; |
205 |
} |
206 |
if (isview(s)) { |
207 |
ph->gotview += sscanview(&ph->vw, s); |
208 |
return; |
209 |
} |
210 |
} |
211 |
|
212 |
|
213 |
addpicz(pcf, zbf) /* add a picture + depth-buffer */ |
214 |
char *pcf, *zbf; |
215 |
{ |
216 |
FILE *pfp; |
217 |
int zfd; |
218 |
COLR *cscn; |
219 |
float *zscn; |
220 |
struct phead phd; |
221 |
int eshft; |
222 |
double emult; |
223 |
RESOLU prs; |
224 |
FLOAT vl[2]; |
225 |
FVECT ro, rd; |
226 |
double aftd; |
227 |
COLOR ctmp; |
228 |
int j; |
229 |
register int i; |
230 |
/* open files */ |
231 |
if ((pfp = fopen(pcf, "r")) == NULL) { |
232 |
sprintf(errmsg, "cannot open picture file \"%s\"", pcf); |
233 |
error(SYSTEM, pcf); |
234 |
} |
235 |
if ((zfd = open(zbf, O_RDONLY)) < 0) { |
236 |
sprintf(errmsg, "cannot open depth file \"%s\"", zbf); |
237 |
error(SYSTEM, pcf); |
238 |
} |
239 |
/* load picture header */ |
240 |
copystruct(&phd.vw, &stdview); |
241 |
phd.expos = 1.0; |
242 |
phd.badfmt = phd.gotview = phd.altprims = 0; |
243 |
if (getheader(pfp, picheadline, &phd) < 0 || |
244 |
phd.badfmt || !fgetsresolu(&prs, pfp)) { |
245 |
sprintf(errmsg, "bad format for picture file \"%s\"", pcf); |
246 |
error(USER, errmsg); |
247 |
} |
248 |
if (!phd.gotview || setview(&phd.vw) != NULL) { |
249 |
sprintf(errmsg, "missing/illegal view in picture \"%s\"", |
250 |
pcf); |
251 |
error(USER, errmsg); |
252 |
} |
253 |
if (phd.altprims) { |
254 |
sprintf(errmsg, "ignoring primary values in picture \"%s\"", |
255 |
pcf); |
256 |
error(WARNING, errmsg); |
257 |
} |
258 |
/* figure out what to do about exposure */ |
259 |
if (phd.expos < 0.99 | phd.expos > 1.01) { |
260 |
emult = -log(phd.expos)/log(2.); |
261 |
eshft = emult >= 0. ? emult+.5 : emult-.5; |
262 |
emult -= (double)eshft; |
263 |
if (emult <= 0.01 & emult >= -0.01) |
264 |
emult = -1.; |
265 |
else { |
266 |
emult = 1./phd.expos; |
267 |
eshft = 0; |
268 |
} |
269 |
} else { |
270 |
emult = -1.; |
271 |
eshft = 0; |
272 |
} |
273 |
/* allocate buffers */ |
274 |
cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR)); |
275 |
zscn = (float *)malloc(scanlen(&prs)*sizeof(float)); |
276 |
if (cscn == NULL | zscn == NULL) |
277 |
error(SYSTEM, "out of memory in addpicz"); |
278 |
/* read and process each scanline */ |
279 |
for (j = 0; j < numscans(&prs); j++) { |
280 |
i = scanlen(&prs); /* read colrs */ |
281 |
if (freadcolrs(cscn, i, pfp) < 0) { |
282 |
sprintf(errmsg, "error reading picture \"%s\"", pcf); |
283 |
error(USER, errmsg); |
284 |
} |
285 |
if (eshft) /* shift exposure */ |
286 |
shiftcolrs(cscn, i, eshft); |
287 |
i *= sizeof(float); /* read depth */ |
288 |
if (read(zfd, (char *)zscn, i) != i) { |
289 |
sprintf(errmsg, "error reading depth file \"%s\"", zbf); |
290 |
error(USER, errmsg); |
291 |
} |
292 |
for (i = scanlen(&prs); i--; ) { /* do each pixel */ |
293 |
pix2loc(vl, &prs, i, j); |
294 |
aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]); |
295 |
if (aftd < -FTINY) |
296 |
continue; /* off view */ |
297 |
if (aftd > FTINY && zscn[i] > aftd) |
298 |
continue; /* aft clipped */ |
299 |
if (emult > 0.) { /* whatta pain */ |
300 |
colr_color(ctmp, cscn[i]); |
301 |
scalecolor(ctmp, emult); |
302 |
setcolr(cscn[i], colval(ctmp,RED), |
303 |
colval(ctmp,GRN), colval(ctmp,BLU)); |
304 |
} |
305 |
addray(ro, rd, (double)zscn[i], cscn[i]); |
306 |
} |
307 |
} |
308 |
/* clean up */ |
309 |
free((char *)cscn); |
310 |
free((char *)zscn); |
311 |
fclose(pfp); |
312 |
close(zfd); |
313 |
} |
314 |
|
315 |
|
316 |
eputs(s) /* put error message to stderr */ |
317 |
register char *s; |
318 |
{ |
319 |
static int midline = 0; |
320 |
|
321 |
if (!*s) |
322 |
return; |
323 |
if (!midline++) { /* prepend line with program name */ |
324 |
fputs(progname, stderr); |
325 |
fputs(": ", stderr); |
326 |
} |
327 |
fputs(s, stderr); |
328 |
if (s[strlen(s)-1] == '\n') { |
329 |
fflush(stderr); |
330 |
midline = 0; |
331 |
} |
332 |
} |
333 |
|
334 |
|
335 |
quit(code) /* exit the program gracefully */ |
336 |
int code; |
337 |
{ |
338 |
hdsync(NULL, 1); /* write out any buffered data */ |
339 |
exit(code); |
340 |
} |