ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.8
Committed: Fri Jan 23 09:05:14 1998 UTC (26 years, 2 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.7: +4 -4 lines
Log Message:
changed -c option to -u and -f option to -d

File Contents

# User Rev Content
1 gregl 3.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 gregl 3.7 #ifndef BLOADSIZE
16     #define BLOADSIZE 1024 /* number of input beams to load at a time */
17     #endif
18    
19 gregl 3.5 int checkdepth = 1; /* check depth (!-f option)? */
20     int checkrepeats = 0; /* check for repeats (-c option)? */
21 gregl 3.1 int frompicz; /* input from pictures & depth-buffers? */
22     int noutsects; /* number of output sections */
23 gregl 3.2 char obstr, unobstr; /* flag pointer values */
24 gregl 3.1
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 gregl 3.5 frompicz = -1;
36     for (i = 2; i < argc && argv[i][0] == '-'; i++)
37     switch (argv[i][1]) {
38 gregl 3.8 case 'u':
39 gregl 3.5 checkrepeats = 1;
40     break;
41 gregl 3.8 case 'd':
42 gregl 3.5 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 gregl 3.1 goto userr;
55 gregl 3.5 if (frompicz && (argc-i)%2)
56 gregl 3.1 goto userr;
57     noutsects = openholo(argv[1], 1);
58     if (frompicz)
59 gregl 3.5 for ( ; i < argc; i += 2)
60 gregl 3.1 addpicz(argv[i], argv[i+1]);
61     else
62 gregl 3.5 for ( ; i < argc; i++)
63 gregl 3.1 addholo(argv[i]);
64     quit(0);
65     userr:
66 gregl 3.8 fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n",
67 gregl 3.1 progname);
68 gregl 3.8 fprintf(stderr, " Or: %s output.hdk [-u][-d] -pz inp1.pic inp1.zbf ..\n",
69 gregl 3.5 progname);
70 gregl 3.1 exit(1);
71     }
72    
73    
74 gregl 3.2 #define H_BADF 01
75     #define H_OBST 02
76     #define H_OBSF 04
77    
78     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 gregl 3.1 return;
90     }
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     return;
101     }
102     }
103    
104     int
105     openholo(fname, append) /* open existing holodeck file for i/o */
106     char *fname;
107     int append;
108     {
109     extern long ftell();
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 gregl 3.2 if (getheader(fp, holheadline, &hflags) < 0 ||
123     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 gregl 3.3 lseek(fd, 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     if (rv->d == dc &&
174     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     addholo(hdf) /* add a holodeck file */
192     char *hdf;
193     {
194 gregl 3.7 HDBEAMI hbl[BLOADSIZE];
195 gregl 3.1 int fd;
196     register HOLO *hp;
197     register BEAM *bp;
198     GCOORD gc[2];
199     FVECT ro, rd;
200     double d;
201 gregl 3.7 int i, j, n, li;
202 gregl 3.1 register int k;
203 gregl 3.4 /* open the holodeck for reading */
204     openholo(hdf, 0);
205 gregl 3.1 fd = hdlist[noutsects]->fd; /* remember the file handle */
206     while ((hp = hdlist[noutsects]) != NULL) { /* load each section */
207 gregl 3.7 for (j = 0; j < nbeams(hp); j++) { /* load each beam */
208     if (!(li = j % BLOADSIZE)) { /* optimize order */
209     if (j+BLOADSIZE > nbeams(hp))
210     k = n = nbeams(hp) - j;
211     else
212     k = n = BLOADSIZE;
213     while (k--) {
214     hbl[k].h = hp;
215     hbl[k].b = j+k+1;
216     }
217     qsort((char *)hbl, n,
218     sizeof(HDBEAMI), hdfilord);
219     }
220     if ((bp = hdgetbeam(hp, hbl[li].b)) != NULL) {
221     hdbcoord(gc, hp, hbl[li].b);
222 gregl 3.1 for (k = bp->nrm; k--; ) {
223 gregl 3.2 d = hdray(ro, rd,
224     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 gregl 3.1 addray(ro, rd, d, hdbray(bp)[k].v);
231     }
232 gregl 3.7 hdfreebeam(hp, hbl[li].b); /* free beam */
233 gregl 3.1 }
234 gregl 3.7 }
235 gregl 3.1 hddone(hp); /* free the section */
236     }
237     close(fd); /* close the file */
238     }
239    
240    
241     struct phead {
242     VIEW vw;
243     double expos;
244     short gotview;
245     short badfmt;
246     short altprims;
247     };
248    
249    
250     picheadline(s, ph) /* process picture header line */
251     char *s;
252     struct phead *ph;
253     {
254     char fmt[32];
255    
256     if (formatval(fmt, s)) {
257     ph->badfmt = strcmp(fmt, COLRFMT);
258     return;
259     }
260     if (isprims(s)) {
261     ph->altprims++; /* don't want to deal with this */
262     return;
263     }
264     if (isexpos(s)) {
265     ph->expos *= exposval(s);
266     return;
267     }
268     if (isview(s)) {
269     ph->gotview += sscanview(&ph->vw, s);
270     return;
271     }
272     }
273    
274    
275     addpicz(pcf, zbf) /* add a picture + depth-buffer */
276     char *pcf, *zbf;
277     {
278     FILE *pfp;
279     int zfd;
280     COLR *cscn;
281     float *zscn;
282     struct phead phd;
283     int eshft;
284     double emult;
285     RESOLU prs;
286     FLOAT vl[2];
287     FVECT ro, rd;
288     double aftd;
289     COLOR ctmp;
290     int j;
291     register int i;
292     /* open files */
293     if ((pfp = fopen(pcf, "r")) == NULL) {
294     sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
295     error(SYSTEM, pcf);
296     }
297     if ((zfd = open(zbf, O_RDONLY)) < 0) {
298     sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
299     error(SYSTEM, pcf);
300     }
301     /* load picture header */
302     copystruct(&phd.vw, &stdview);
303     phd.expos = 1.0;
304     phd.badfmt = phd.gotview = phd.altprims = 0;
305     if (getheader(pfp, picheadline, &phd) < 0 ||
306     phd.badfmt || !fgetsresolu(&prs, pfp)) {
307     sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
308     error(USER, errmsg);
309     }
310     if (!phd.gotview || setview(&phd.vw) != NULL) {
311     sprintf(errmsg, "missing/illegal view in picture \"%s\"",
312     pcf);
313     error(USER, errmsg);
314     }
315     if (phd.altprims) {
316     sprintf(errmsg, "ignoring primary values in picture \"%s\"",
317     pcf);
318     error(WARNING, errmsg);
319     }
320     /* figure out what to do about exposure */
321     if (phd.expos < 0.99 | phd.expos > 1.01) {
322     emult = -log(phd.expos)/log(2.);
323     eshft = emult >= 0. ? emult+.5 : emult-.5;
324     emult -= (double)eshft;
325     if (emult <= 0.01 & emult >= -0.01)
326     emult = -1.;
327     else {
328     emult = 1./phd.expos;
329     eshft = 0;
330     }
331     } else {
332     emult = -1.;
333     eshft = 0;
334     }
335     /* allocate buffers */
336     cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
337     zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
338     if (cscn == NULL | zscn == NULL)
339     error(SYSTEM, "out of memory in addpicz");
340     /* read and process each scanline */
341     for (j = 0; j < numscans(&prs); j++) {
342     i = scanlen(&prs); /* read colrs */
343     if (freadcolrs(cscn, i, pfp) < 0) {
344     sprintf(errmsg, "error reading picture \"%s\"", pcf);
345     error(USER, errmsg);
346     }
347     if (eshft) /* shift exposure */
348     shiftcolrs(cscn, i, eshft);
349     i *= sizeof(float); /* read depth */
350     if (read(zfd, (char *)zscn, i) != i) {
351     sprintf(errmsg, "error reading depth file \"%s\"", zbf);
352     error(USER, errmsg);
353     }
354     for (i = scanlen(&prs); i--; ) { /* do each pixel */
355     pix2loc(vl, &prs, i, j);
356     aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
357     if (aftd < -FTINY)
358     continue; /* off view */
359     if (aftd > FTINY && zscn[i] > aftd)
360     continue; /* aft clipped */
361     if (emult > 0.) { /* whatta pain */
362     colr_color(ctmp, cscn[i]);
363     scalecolor(ctmp, emult);
364     setcolr(cscn[i], colval(ctmp,RED),
365     colval(ctmp,GRN), colval(ctmp,BLU));
366     }
367     addray(ro, rd, (double)zscn[i], cscn[i]);
368     }
369     }
370     /* clean up */
371     free((char *)cscn);
372     free((char *)zscn);
373     fclose(pfp);
374     close(zfd);
375     }
376    
377    
378     eputs(s) /* put error message to stderr */
379     register char *s;
380     {
381     static int midline = 0;
382    
383     if (!*s)
384     return;
385     if (!midline++) { /* prepend line with program name */
386     fputs(progname, stderr);
387     fputs(": ", stderr);
388     }
389     fputs(s, stderr);
390     if (s[strlen(s)-1] == '\n') {
391     fflush(stderr);
392     midline = 0;
393     }
394     }
395    
396    
397     quit(code) /* exit the program gracefully */
398     int code;
399     {
400     hdsync(NULL, 1); /* write out any buffered data */
401     exit(code);
402     }