ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.14
Committed: Mon Mar 8 17:32:26 1999 UTC (25 years ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.13: +2 -3 lines
Log Message:
removed unnecessary call to hdfreebeams() that increased fragmentation

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 gwlarson 3.14 int checkdepth = 1; /* check depth (!-d option)? */
20     int checkrepeats = 0; /* check for repeats (-u 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.11 return(0);
240 gwlarson 3.10 }
241    
242     addholo(hdf) /* add a holodeck file */
243     char *hdf;
244     {
245     int fd;
246 gregl 3.4 /* open the holodeck for reading */
247     openholo(hdf, 0);
248 gregl 3.1 fd = hdlist[noutsects]->fd; /* remember the file handle */
249 gwlarson 3.10 while (hdlist[noutsects] != NULL) { /* load each section */
250     /* clump the beams */
251 gwlarson 3.11 clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
252 gwlarson 3.10 hddone(hdlist[noutsects]); /* free the section */
253 gregl 3.1 }
254 gwlarson 3.10 close(fd); /* close input file */
255 gwlarson 3.13 hdflush(NULL); /* flush output */
256 gregl 3.1 }
257    
258    
259     struct phead {
260     VIEW vw;
261     double expos;
262     short gotview;
263     short badfmt;
264     short altprims;
265     };
266    
267    
268 gwlarson 3.9 int
269 gregl 3.1 picheadline(s, ph) /* process picture header line */
270     char *s;
271     struct phead *ph;
272     {
273     char fmt[32];
274    
275     if (formatval(fmt, s)) {
276     ph->badfmt = strcmp(fmt, COLRFMT);
277 gwlarson 3.9 return(0);
278 gregl 3.1 }
279     if (isprims(s)) {
280     ph->altprims++; /* don't want to deal with this */
281 gwlarson 3.9 return(0);
282 gregl 3.1 }
283     if (isexpos(s)) {
284     ph->expos *= exposval(s);
285 gwlarson 3.9 return(0);
286 gregl 3.1 }
287     if (isview(s)) {
288     ph->gotview += sscanview(&ph->vw, s);
289 gwlarson 3.9 return(0);
290 gregl 3.1 }
291 gwlarson 3.9 return(0);
292 gregl 3.1 }
293    
294    
295     addpicz(pcf, zbf) /* add a picture + depth-buffer */
296     char *pcf, *zbf;
297     {
298     FILE *pfp;
299     int zfd;
300     COLR *cscn;
301     float *zscn;
302     struct phead phd;
303     int eshft;
304     double emult;
305     RESOLU prs;
306     FLOAT vl[2];
307     FVECT ro, rd;
308     double aftd;
309     COLOR ctmp;
310     int j;
311     register int i;
312     /* open files */
313     if ((pfp = fopen(pcf, "r")) == NULL) {
314     sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
315     error(SYSTEM, pcf);
316     }
317     if ((zfd = open(zbf, O_RDONLY)) < 0) {
318     sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
319     error(SYSTEM, pcf);
320     }
321     /* load picture header */
322     copystruct(&phd.vw, &stdview);
323     phd.expos = 1.0;
324     phd.badfmt = phd.gotview = phd.altprims = 0;
325     if (getheader(pfp, picheadline, &phd) < 0 ||
326     phd.badfmt || !fgetsresolu(&prs, pfp)) {
327     sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
328     error(USER, errmsg);
329     }
330     if (!phd.gotview || setview(&phd.vw) != NULL) {
331     sprintf(errmsg, "missing/illegal view in picture \"%s\"",
332     pcf);
333     error(USER, errmsg);
334     }
335     if (phd.altprims) {
336     sprintf(errmsg, "ignoring primary values in picture \"%s\"",
337     pcf);
338     error(WARNING, errmsg);
339     }
340     /* figure out what to do about exposure */
341     if (phd.expos < 0.99 | phd.expos > 1.01) {
342     emult = -log(phd.expos)/log(2.);
343     eshft = emult >= 0. ? emult+.5 : emult-.5;
344     emult -= (double)eshft;
345     if (emult <= 0.01 & emult >= -0.01)
346     emult = -1.;
347     else {
348     emult = 1./phd.expos;
349     eshft = 0;
350     }
351     } else {
352     emult = -1.;
353     eshft = 0;
354     }
355     /* allocate buffers */
356     cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
357     zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
358     if (cscn == NULL | zscn == NULL)
359     error(SYSTEM, "out of memory in addpicz");
360     /* read and process each scanline */
361     for (j = 0; j < numscans(&prs); j++) {
362     i = scanlen(&prs); /* read colrs */
363     if (freadcolrs(cscn, i, pfp) < 0) {
364     sprintf(errmsg, "error reading picture \"%s\"", pcf);
365     error(USER, errmsg);
366     }
367     if (eshft) /* shift exposure */
368     shiftcolrs(cscn, i, eshft);
369     i *= sizeof(float); /* read depth */
370     if (read(zfd, (char *)zscn, i) != i) {
371     sprintf(errmsg, "error reading depth file \"%s\"", zbf);
372     error(USER, errmsg);
373     }
374     for (i = scanlen(&prs); i--; ) { /* do each pixel */
375     pix2loc(vl, &prs, i, j);
376     aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
377     if (aftd < -FTINY)
378     continue; /* off view */
379     if (aftd > FTINY && zscn[i] > aftd)
380     continue; /* aft clipped */
381     if (emult > 0.) { /* whatta pain */
382     colr_color(ctmp, cscn[i]);
383     scalecolor(ctmp, emult);
384     setcolr(cscn[i], colval(ctmp,RED),
385     colval(ctmp,GRN), colval(ctmp,BLU));
386     }
387     addray(ro, rd, (double)zscn[i], cscn[i]);
388     }
389     }
390 gwlarson 3.11 /* write output and free beams */
391     hdflush(NULL);
392 gregl 3.1 /* clean up */
393     free((char *)cscn);
394     free((char *)zscn);
395     fclose(pfp);
396     close(zfd);
397     }
398    
399    
400     eputs(s) /* put error message to stderr */
401     register char *s;
402     {
403     static int midline = 0;
404    
405     if (!*s)
406     return;
407     if (!midline++) { /* prepend line with program name */
408     fputs(progname, stderr);
409     fputs(": ", stderr);
410     }
411     fputs(s, stderr);
412     if (s[strlen(s)-1] == '\n') {
413     fflush(stderr);
414     midline = 0;
415     }
416     }
417    
418    
419     quit(code) /* exit the program gracefully */
420     int code;
421     {
422     hdsync(NULL, 1); /* write out any buffered data */
423     exit(code);
424     }