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