ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.33
Committed: Thu Nov 7 23:17:58 2019 UTC (4 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 3.32: +9 -3 lines
Log Message:
Added check for byte-swapping

File Contents

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