ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.22
Committed: Thu Jan 1 11:21:55 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.21: +69 -39 lines
Log Message:
Ansification and prototypes.

File Contents

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