ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.21
Committed: Wed Oct 22 02:06:34 2003 UTC (20 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.20: +2 -2 lines
Log Message:
Fewer complaints if "platform.h" precedes "standard.h"

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.21 static const char RCSid[] = "$Id: rhcopy.c,v 3.20 2003/10/20 16:01:55 greg Exp $";
3 gregl 3.1 #endif
4     /*
5     * Copy data into a holodeck file
6     */
7    
8 greg 3.21 #include "platform.h"
9 gregl 3.1 #include "holo.h"
10     #include "view.h"
11    
12 gwlarson 3.10 #ifndef BKBSIZE
13     #define BKBSIZE 256 /* beam clump size (kilobytes) */
14 gregl 3.7 #endif
15    
16 gwlarson 3.14 int checkdepth = 1; /* check depth (!-d option)? */
17     int checkrepeats = 0; /* check for repeats (-u option)? */
18 gregl 3.1 int frompicz; /* input from pictures & depth-buffers? */
19     int noutsects; /* number of output sections */
20 gregl 3.2 char obstr, unobstr; /* flag pointer values */
21 gregl 3.1
22     char *progname; /* global argv[0] */
23    
24    
25     main(argc, argv)
26     int argc;
27     char *argv[];
28     {
29     int i;
30    
31     progname = argv[0];
32 gregl 3.5 frompicz = -1;
33     for (i = 2; i < argc && argv[i][0] == '-'; i++)
34     switch (argv[i][1]) {
35 gregl 3.8 case 'u':
36 gregl 3.5 checkrepeats = 1;
37     break;
38 gregl 3.8 case 'd':
39 gregl 3.5 checkdepth = 0;
40     break;
41     case 'h':
42     frompicz = 0;
43     break;
44     case 'p':
45     frompicz = 1;
46     break;
47     default:
48     goto userr;
49     }
50     if (i >= argc || frompicz < 0)
51 gregl 3.1 goto userr;
52 gregl 3.5 if (frompicz && (argc-i)%2)
53 gregl 3.1 goto userr;
54     noutsects = openholo(argv[1], 1);
55 gwlarson 3.10 if (frompicz) {
56 gregl 3.5 for ( ; i < argc; i += 2)
57 gregl 3.1 addpicz(argv[i], argv[i+1]);
58 gwlarson 3.10 } else {
59     if (BKBSIZE*1024*1.5 > hdcachesize)
60     hdcachesize = BKBSIZE*1024*1.5;
61 gregl 3.5 for ( ; i < argc; i++)
62 gregl 3.1 addholo(argv[i]);
63 gwlarson 3.10 }
64 gregl 3.1 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 gwlarson 3.10 fprintf(stderr, " Or: %s output.hdk [-u][-d] -p 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 gwlarson 3.9 int
79 gregl 3.2 holheadline(s, hf) /* check holodeck header line */
80 gregl 3.1 register char *s;
81 gregl 3.2 int *hf;
82 gregl 3.1 {
83     char fmt[32];
84    
85     if (formatval(fmt, s)) {
86 gregl 3.2 if (strcmp(fmt, HOLOFMT))
87     *hf |= H_BADF;
88     else
89     *hf &= ~H_BADF;
90 gwlarson 3.9 return(0);
91 gregl 3.1 }
92     if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
93     s += 13;
94     while (*s == ' ') s++;
95 schorsch 3.19 if ((*s == 't') | (*s == 'T'))
96 gregl 3.2 *hf |= H_OBST;
97 schorsch 3.19 else if ((*s == 'f') | (*s == 'F'))
98 gregl 3.2 *hf |= H_OBSF;
99 gregl 3.1 else
100     error(WARNING, "bad OBSTRUCTIONS value in holodeck");
101 gwlarson 3.9 return(0);
102 gregl 3.1 }
103 gwlarson 3.9 return(0);
104 gregl 3.1 }
105    
106     int
107     openholo(fname, append) /* open existing holodeck file for i/o */
108     char *fname;
109     int append;
110     {
111     FILE *fp;
112     int fd;
113 gregl 3.2 int hflags = 0;
114 gregl 3.3 long nextloc;
115 gregl 3.1 int n;
116     /* open holodeck file */
117     if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) {
118     sprintf(errmsg, "cannot open \"%s\" for %s", fname,
119     append ? "appending" : "reading");
120     error(SYSTEM, errmsg);
121     }
122     /* check header and magic number */
123 greg 3.15 if (getheader(fp, holheadline, (char *)&hflags) < 0 ||
124 gregl 3.2 hflags&H_BADF || getw(fp) != HOLOMAGIC) {
125 gregl 3.1 sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
126     error(USER, errmsg);
127     }
128     fd = dup(fileno(fp)); /* dup file handle */
129     nextloc = ftell(fp); /* get stdio position */
130     fclose(fp); /* done with stdio */
131     for (n = 0; nextloc > 0L; n++) { /* initialize each section */
132 greg 3.20 lseek(fd, (off_t)nextloc, SEEK_SET);
133 gregl 3.1 read(fd, (char *)&nextloc, sizeof(nextloc));
134 gregl 3.2 hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
135     hflags&H_OBSF ? &unobstr : (char *)NULL;
136 gregl 3.1 }
137     return(n);
138     }
139    
140 gregl 3.2 #undef H_BADF
141     #undef H_OBST
142     #undef H_OBSF
143 gregl 3.1
144 gregl 3.2
145 gregl 3.1 addray(ro, rd, d, cv) /* add a ray to our output holodeck */
146     FVECT ro, rd;
147     double d;
148     COLR cv;
149     {
150 gregl 3.5 int sn, bi, n;
151 gregl 3.1 register HOLO *hp;
152     GCOORD gc[2];
153     BYTE rr[2][2];
154 gregl 3.5 BEAM *bp;
155 gregl 3.1 double d0, d1;
156 gregl 3.5 unsigned dc;
157 gregl 3.1 register RAYVAL *rv;
158     /* check each output section */
159     for (sn = noutsects; sn--; ) {
160     hp = hdlist[sn];
161     d0 = hdinter(gc, rr, &d1, hp, ro, rd);
162     if (d <= d0 || d1 < -0.001)
163     continue; /* missed section */
164 gregl 3.5 if (checkdepth) { /* check depth */
165     if (hp->priv == &obstr && d0 < -0.001)
166     continue; /* ray starts too late */
167     if (hp->priv == &unobstr && d < 0.999*d1)
168     continue; /* ray ends too soon */
169     }
170     dc = hdcode(hp, d-d0);
171     bi = hdbindex(hp, gc); /* check for duplicates */
172     if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) {
173     for (n = bp->nrm, rv = hdbray(bp); n--; rv++)
174 gwlarson 3.12 if ((hp->priv != NULL || rv->d == dc) &&
175 gregl 3.5 rv->r[0][0] == rr[0][0] &&
176     rv->r[0][1] == rr[0][1] &&
177     rv->r[1][0] == rr[1][0] &&
178     rv->r[1][1] == rr[1][1])
179     break;
180     if (n >= 0)
181     continue; /* found a matching ray */
182     }
183     rv = hdnewrays(hp, bi, 1);
184     rv->d = dc;
185 gregl 3.1 rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
186     rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
187     copycolr(rv->v, cv);
188     }
189     }
190    
191    
192 gwlarson 3.11 static BEAMI *beamdir;
193    
194     static int
195     bpcmp(b1p, b2p) /* compare beam positions on disk */
196     int *b1p, *b2p;
197 gregl 3.1 {
198 greg 3.16 register off_t pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
199 gwlarson 3.11
200     if (pdif > 0L) return(1);
201     if (pdif < 0L) return(-1);
202     return(0);
203     }
204    
205     static int
206     addclump(hp, bq, nb) /* transfer the given clump and free */
207     HOLO *hp;
208     int *bq, nb;
209     {
210 gregl 3.1 GCOORD gc[2];
211     FVECT ro, rd;
212     double d;
213 gwlarson 3.11 int i;
214 gregl 3.1 register int k;
215 gwlarson 3.11 register BEAM *bp;
216     /* sort based on file position */
217     beamdir = hp->bi;
218     qsort((char *)bq, nb, sizeof(*bq), bpcmp);
219     /* transfer each beam */
220     for (i = 0; i < nb; i++) {
221     bp = hdgetbeam(hp, bq[i]);
222     hdbcoord(gc, hp, bq[i]);
223     /* add each ray to output */
224     for (k = bp->nrm; k--; ) {
225     d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
226     if (hp->priv == &unobstr)
227     VSUM(ro, ro, rd, d);
228     else
229     d = 0.;
230     d = hddepth(hp, hdbray(bp)[k].d) - d;
231     addray(ro, rd, d, hdbray(bp)[k].v);
232     }
233     hdfreebeam(hp, bq[i]); /* free the beam */
234 gwlarson 3.10 }
235 gwlarson 3.11 return(0);
236 gwlarson 3.10 }
237    
238     addholo(hdf) /* add a holodeck file */
239     char *hdf;
240     {
241     int fd;
242 gregl 3.4 /* open the holodeck for reading */
243     openholo(hdf, 0);
244 gregl 3.1 fd = hdlist[noutsects]->fd; /* remember the file handle */
245 gwlarson 3.10 while (hdlist[noutsects] != NULL) { /* load each section */
246     /* clump the beams */
247 gwlarson 3.11 clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
248 gwlarson 3.10 hddone(hdlist[noutsects]); /* free the section */
249 gregl 3.1 }
250 gwlarson 3.10 close(fd); /* close input file */
251 gwlarson 3.13 hdflush(NULL); /* flush output */
252 gregl 3.1 }
253    
254    
255     struct phead {
256     VIEW vw;
257     double expos;
258     short gotview;
259     short badfmt;
260     short altprims;
261     };
262    
263    
264 gwlarson 3.9 int
265 gregl 3.1 picheadline(s, ph) /* process picture header line */
266     char *s;
267     struct phead *ph;
268     {
269     char fmt[32];
270    
271     if (formatval(fmt, s)) {
272     ph->badfmt = strcmp(fmt, COLRFMT);
273 gwlarson 3.9 return(0);
274 gregl 3.1 }
275     if (isprims(s)) {
276     ph->altprims++; /* don't want to deal with this */
277 gwlarson 3.9 return(0);
278 gregl 3.1 }
279     if (isexpos(s)) {
280     ph->expos *= exposval(s);
281 gwlarson 3.9 return(0);
282 gregl 3.1 }
283     if (isview(s)) {
284     ph->gotview += sscanview(&ph->vw, s);
285 gwlarson 3.9 return(0);
286 gregl 3.1 }
287 gwlarson 3.9 return(0);
288 gregl 3.1 }
289    
290    
291     addpicz(pcf, zbf) /* add a picture + depth-buffer */
292     char *pcf, *zbf;
293     {
294     FILE *pfp;
295     int zfd;
296     COLR *cscn;
297     float *zscn;
298     struct phead phd;
299     int eshft;
300     double emult;
301     RESOLU prs;
302 schorsch 3.17 RREAL vl[2];
303 gregl 3.1 FVECT ro, rd;
304     double aftd;
305     COLOR ctmp;
306     int j;
307     register int i;
308     /* open files */
309     if ((pfp = fopen(pcf, "r")) == NULL) {
310     sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
311     error(SYSTEM, pcf);
312     }
313     if ((zfd = open(zbf, O_RDONLY)) < 0) {
314     sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
315     error(SYSTEM, pcf);
316     }
317     /* load picture header */
318 schorsch 3.18 phd.vw = stdview;
319 gregl 3.1 phd.expos = 1.0;
320     phd.badfmt = phd.gotview = phd.altprims = 0;
321 greg 3.15 if (getheader(pfp, picheadline, (char *)&phd) < 0 ||
322 gregl 3.1 phd.badfmt || !fgetsresolu(&prs, pfp)) {
323     sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
324     error(USER, errmsg);
325     }
326     if (!phd.gotview || setview(&phd.vw) != NULL) {
327     sprintf(errmsg, "missing/illegal view in picture \"%s\"",
328     pcf);
329     error(USER, errmsg);
330     }
331     if (phd.altprims) {
332     sprintf(errmsg, "ignoring primary values in picture \"%s\"",
333     pcf);
334     error(WARNING, errmsg);
335     }
336     /* figure out what to do about exposure */
337 schorsch 3.19 if ((phd.expos < 0.99) | (phd.expos > 1.01)) {
338 gregl 3.1 emult = -log(phd.expos)/log(2.);
339     eshft = emult >= 0. ? emult+.5 : emult-.5;
340     emult -= (double)eshft;
341 schorsch 3.19 if ((emult <= 0.01) & (emult >= -0.01))
342 gregl 3.1 emult = -1.;
343     else {
344     emult = 1./phd.expos;
345     eshft = 0;
346     }
347     } else {
348     emult = -1.;
349     eshft = 0;
350     }
351     /* allocate buffers */
352     cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
353     zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
354 schorsch 3.19 if ((cscn == NULL) | (zscn == NULL))
355 gregl 3.1 error(SYSTEM, "out of memory in addpicz");
356     /* read and process each scanline */
357     for (j = 0; j < numscans(&prs); j++) {
358     i = scanlen(&prs); /* read colrs */
359     if (freadcolrs(cscn, i, pfp) < 0) {
360     sprintf(errmsg, "error reading picture \"%s\"", pcf);
361     error(USER, errmsg);
362     }
363     if (eshft) /* shift exposure */
364     shiftcolrs(cscn, i, eshft);
365     i *= sizeof(float); /* read depth */
366     if (read(zfd, (char *)zscn, i) != i) {
367     sprintf(errmsg, "error reading depth file \"%s\"", zbf);
368     error(USER, errmsg);
369     }
370     for (i = scanlen(&prs); i--; ) { /* do each pixel */
371     pix2loc(vl, &prs, i, j);
372     aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
373     if (aftd < -FTINY)
374     continue; /* off view */
375     if (aftd > FTINY && zscn[i] > aftd)
376     continue; /* aft clipped */
377     if (emult > 0.) { /* whatta pain */
378     colr_color(ctmp, cscn[i]);
379     scalecolor(ctmp, emult);
380     setcolr(cscn[i], colval(ctmp,RED),
381     colval(ctmp,GRN), colval(ctmp,BLU));
382     }
383     addray(ro, rd, (double)zscn[i], cscn[i]);
384     }
385     }
386 gwlarson 3.11 /* write output and free beams */
387     hdflush(NULL);
388 gregl 3.1 /* clean up */
389 greg 3.15 free((void *)cscn);
390     free((void *)zscn);
391 gregl 3.1 fclose(pfp);
392     close(zfd);
393     }
394    
395    
396 greg 3.15 void
397 gregl 3.1 eputs(s) /* put error message to stderr */
398     register char *s;
399     {
400     static int midline = 0;
401    
402     if (!*s)
403     return;
404     if (!midline++) { /* prepend line with program name */
405     fputs(progname, stderr);
406     fputs(": ", stderr);
407     }
408     fputs(s, stderr);
409     if (s[strlen(s)-1] == '\n') {
410     fflush(stderr);
411     midline = 0;
412     }
413     }
414    
415    
416 greg 3.15 void
417 gregl 3.1 quit(code) /* exit the program gracefully */
418     int code;
419     {
420     hdsync(NULL, 1); /* write out any buffered data */
421     exit(code);
422     }