ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.10
Committed: Mon Nov 9 17:11:40 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.9: +39 -44 lines
Log Message:
moved routines to clumpbeams.c and made rhcopy clump beams

File Contents

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