ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.13
Committed: Mon Feb 1 10:22:46 1999 UTC (25 years, 2 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.12: +2 -1 lines
Log Message:
made caching code slightly more efficient

File Contents

# User Rev Content
1 gwlarson 3.12 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2 gregl 3.1
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 gwlarson 3.10 #ifndef BKBSIZE
16     #define BKBSIZE 256 /* beam clump size (kilobytes) */
17 gregl 3.7 #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 gwlarson 3.10 if (frompicz) {
59 gregl 3.5 for ( ; i < argc; i += 2)
60 gregl 3.1 addpicz(argv[i], argv[i+1]);
61 gwlarson 3.10 } else {
62     if (BKBSIZE*1024*1.5 > hdcachesize)
63     hdcachesize = BKBSIZE*1024*1.5;
64 gregl 3.5 for ( ; i < argc; i++)
65 gregl 3.1 addholo(argv[i]);
66 gwlarson 3.10 }
67 gregl 3.1 quit(0);
68     userr:
69 gregl 3.8 fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n",
70 gregl 3.1 progname);
71 gwlarson 3.10 fprintf(stderr, " Or: %s output.hdk [-u][-d] -p inp1.pic inp1.zbf ..\n",
72 gregl 3.5 progname);
73 gregl 3.1 exit(1);
74     }
75    
76    
77 gregl 3.2 #define H_BADF 01
78     #define H_OBST 02
79     #define H_OBSF 04
80    
81 gwlarson 3.9 int
82 gregl 3.2 holheadline(s, hf) /* check holodeck header line */
83 gregl 3.1 register char *s;
84 gregl 3.2 int *hf;
85 gregl 3.1 {
86     char fmt[32];
87    
88     if (formatval(fmt, s)) {
89 gregl 3.2 if (strcmp(fmt, HOLOFMT))
90     *hf |= H_BADF;
91     else
92     *hf &= ~H_BADF;
93 gwlarson 3.9 return(0);
94 gregl 3.1 }
95     if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
96     s += 13;
97     while (*s == ' ') s++;
98     if (*s == 't' | *s == 'T')
99 gregl 3.2 *hf |= H_OBST;
100 gregl 3.1 else if (*s == 'f' | *s == 'F')
101 gregl 3.2 *hf |= H_OBSF;
102 gregl 3.1 else
103     error(WARNING, "bad OBSTRUCTIONS value in holodeck");
104 gwlarson 3.9 return(0);
105 gregl 3.1 }
106 gwlarson 3.9 return(0);
107 gregl 3.1 }
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 gregl 3.2 int hflags = 0;
118 gregl 3.3 long nextloc;
119 gregl 3.1 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 gregl 3.2 if (getheader(fp, holheadline, &hflags) < 0 ||
128     hflags&H_BADF || getw(fp) != HOLOMAGIC) {
129 gregl 3.1 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 gregl 3.3 lseek(fd, nextloc, 0);
137 gregl 3.1 read(fd, (char *)&nextloc, sizeof(nextloc));
138 gregl 3.2 hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
139     hflags&H_OBSF ? &unobstr : (char *)NULL;
140 gregl 3.1 }
141     return(n);
142     }
143    
144 gregl 3.2 #undef H_BADF
145     #undef H_OBST
146     #undef H_OBSF
147 gregl 3.1
148 gregl 3.2
149 gregl 3.1 addray(ro, rd, d, cv) /* add a ray to our output holodeck */
150     FVECT ro, rd;
151     double d;
152     COLR cv;
153     {
154 gregl 3.5 int sn, bi, n;
155 gregl 3.1 register HOLO *hp;
156     GCOORD gc[2];
157     BYTE rr[2][2];
158 gregl 3.5 BEAM *bp;
159 gregl 3.1 double d0, d1;
160 gregl 3.5 unsigned dc;
161 gregl 3.1 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 gregl 3.5 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 gwlarson 3.12 if ((hp->priv != NULL || rv->d == dc) &&
179 gregl 3.5 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 gregl 3.1 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 gwlarson 3.11 static BEAMI *beamdir;
197    
198     static int
199     bpcmp(b1p, b2p) /* compare beam positions on disk */
200     int *b1p, *b2p;
201 gregl 3.1 {
202 gwlarson 3.11 register long pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
203    
204     if (pdif > 0L) return(1);
205     if (pdif < 0L) return(-1);
206     return(0);
207     }
208    
209     static int
210     addclump(hp, bq, nb) /* transfer the given clump and free */
211     HOLO *hp;
212     int *bq, nb;
213     {
214 gregl 3.1 GCOORD gc[2];
215     FVECT ro, rd;
216     double d;
217 gwlarson 3.11 int i;
218 gregl 3.1 register int k;
219 gwlarson 3.11 register BEAM *bp;
220     /* sort based on file position */
221     beamdir = hp->bi;
222     qsort((char *)bq, nb, sizeof(*bq), bpcmp);
223     /* transfer each beam */
224     for (i = 0; i < nb; i++) {
225     bp = hdgetbeam(hp, bq[i]);
226     hdbcoord(gc, hp, bq[i]);
227     /* add each ray to output */
228     for (k = bp->nrm; k--; ) {
229     d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
230     if (hp->priv == &unobstr)
231     VSUM(ro, ro, rd, d);
232     else
233     d = 0.;
234     d = hddepth(hp, hdbray(bp)[k].d) - d;
235     addray(ro, rd, d, hdbray(bp)[k].v);
236     }
237     hdfreebeam(hp, bq[i]); /* free the beam */
238 gwlarson 3.10 }
239 gwlarson 3.13 hdfreebeam(NULL, 0); /* write & free clump */
240 gwlarson 3.11 return(0);
241 gwlarson 3.10 }
242    
243     addholo(hdf) /* add a holodeck file */
244     char *hdf;
245     {
246     int fd;
247 gregl 3.4 /* open the holodeck for reading */
248     openholo(hdf, 0);
249 gregl 3.1 fd = hdlist[noutsects]->fd; /* remember the file handle */
250 gwlarson 3.10 while (hdlist[noutsects] != NULL) { /* load each section */
251     /* clump the beams */
252 gwlarson 3.11 clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
253 gwlarson 3.10 hddone(hdlist[noutsects]); /* free the section */
254 gregl 3.1 }
255 gwlarson 3.10 close(fd); /* close input file */
256 gwlarson 3.13 hdflush(NULL); /* flush output */
257 gregl 3.1 }
258    
259    
260     struct phead {
261     VIEW vw;
262     double expos;
263     short gotview;
264     short badfmt;
265     short altprims;
266     };
267    
268    
269 gwlarson 3.9 int
270 gregl 3.1 picheadline(s, ph) /* process picture header line */
271     char *s;
272     struct phead *ph;
273     {
274     char fmt[32];
275    
276     if (formatval(fmt, s)) {
277     ph->badfmt = strcmp(fmt, COLRFMT);
278 gwlarson 3.9 return(0);
279 gregl 3.1 }
280     if (isprims(s)) {
281     ph->altprims++; /* don't want to deal with this */
282 gwlarson 3.9 return(0);
283 gregl 3.1 }
284     if (isexpos(s)) {
285     ph->expos *= exposval(s);
286 gwlarson 3.9 return(0);
287 gregl 3.1 }
288     if (isview(s)) {
289     ph->gotview += sscanview(&ph->vw, s);
290 gwlarson 3.9 return(0);
291 gregl 3.1 }
292 gwlarson 3.9 return(0);
293 gregl 3.1 }
294    
295    
296     addpicz(pcf, zbf) /* add a picture + depth-buffer */
297     char *pcf, *zbf;
298     {
299     FILE *pfp;
300     int zfd;
301     COLR *cscn;
302     float *zscn;
303     struct phead phd;
304     int eshft;
305     double emult;
306     RESOLU prs;
307     FLOAT vl[2];
308     FVECT ro, rd;
309     double aftd;
310     COLOR ctmp;
311     int j;
312     register int i;
313     /* open files */
314     if ((pfp = fopen(pcf, "r")) == NULL) {
315     sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
316     error(SYSTEM, pcf);
317     }
318     if ((zfd = open(zbf, O_RDONLY)) < 0) {
319     sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
320     error(SYSTEM, pcf);
321     }
322     /* load picture header */
323     copystruct(&phd.vw, &stdview);
324     phd.expos = 1.0;
325     phd.badfmt = phd.gotview = phd.altprims = 0;
326     if (getheader(pfp, picheadline, &phd) < 0 ||
327     phd.badfmt || !fgetsresolu(&prs, pfp)) {
328     sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
329     error(USER, errmsg);
330     }
331     if (!phd.gotview || setview(&phd.vw) != NULL) {
332     sprintf(errmsg, "missing/illegal view in picture \"%s\"",
333     pcf);
334     error(USER, errmsg);
335     }
336     if (phd.altprims) {
337     sprintf(errmsg, "ignoring primary values in picture \"%s\"",
338     pcf);
339     error(WARNING, errmsg);
340     }
341     /* figure out what to do about exposure */
342     if (phd.expos < 0.99 | phd.expos > 1.01) {
343     emult = -log(phd.expos)/log(2.);
344     eshft = emult >= 0. ? emult+.5 : emult-.5;
345     emult -= (double)eshft;
346     if (emult <= 0.01 & emult >= -0.01)
347     emult = -1.;
348     else {
349     emult = 1./phd.expos;
350     eshft = 0;
351     }
352     } else {
353     emult = -1.;
354     eshft = 0;
355     }
356     /* allocate buffers */
357     cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
358     zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
359     if (cscn == NULL | zscn == NULL)
360     error(SYSTEM, "out of memory in addpicz");
361     /* read and process each scanline */
362     for (j = 0; j < numscans(&prs); j++) {
363     i = scanlen(&prs); /* read colrs */
364     if (freadcolrs(cscn, i, pfp) < 0) {
365     sprintf(errmsg, "error reading picture \"%s\"", pcf);
366     error(USER, errmsg);
367     }
368     if (eshft) /* shift exposure */
369     shiftcolrs(cscn, i, eshft);
370     i *= sizeof(float); /* read depth */
371     if (read(zfd, (char *)zscn, i) != i) {
372     sprintf(errmsg, "error reading depth file \"%s\"", zbf);
373     error(USER, errmsg);
374     }
375     for (i = scanlen(&prs); i--; ) { /* do each pixel */
376     pix2loc(vl, &prs, i, j);
377     aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
378     if (aftd < -FTINY)
379     continue; /* off view */
380     if (aftd > FTINY && zscn[i] > aftd)
381     continue; /* aft clipped */
382     if (emult > 0.) { /* whatta pain */
383     colr_color(ctmp, cscn[i]);
384     scalecolor(ctmp, emult);
385     setcolr(cscn[i], colval(ctmp,RED),
386     colval(ctmp,GRN), colval(ctmp,BLU));
387     }
388     addray(ro, rd, (double)zscn[i], cscn[i]);
389     }
390     }
391 gwlarson 3.11 /* write output and free beams */
392     hdflush(NULL);
393 gregl 3.1 /* clean up */
394     free((char *)cscn);
395     free((char *)zscn);
396     fclose(pfp);
397     close(zfd);
398     }
399    
400    
401     eputs(s) /* put error message to stderr */
402     register char *s;
403     {
404     static int midline = 0;
405    
406     if (!*s)
407     return;
408     if (!midline++) { /* prepend line with program name */
409     fputs(progname, stderr);
410     fputs(": ", stderr);
411     }
412     fputs(s, stderr);
413     if (s[strlen(s)-1] == '\n') {
414     fflush(stderr);
415     midline = 0;
416     }
417     }
418    
419    
420     quit(code) /* exit the program gracefully */
421     int code;
422     {
423     hdsync(NULL, 1); /* write out any buffered data */
424     exit(code);
425     }