ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.6
Committed: Wed Jan 7 10:49:12 1998 UTC (26 years, 2 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.5: +2 -2 lines
Log Message:
fixed bug from sorting change (3.4

File Contents

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