ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.4
Committed: Tue Jan 6 22:02:22 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +16 -6 lines
Log Message:
change code to read beams in file order

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