ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
Revision: 3.37
Committed: Tue Dec 19 20:22:36 2023 UTC (4 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.36: +2 -2 lines
Log Message:
perf(rhcopy): Increased default memory cache size

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.37 static const char RCSid[] = "$Id: rhcopy.c,v 3.36 2023/02/06 22:40:21 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 greg 3.34 /* possible operations */
17     #define FROM_HOLO 1 /* copy between holodecks */
18     #define FROM_PICZ 2 /* copy from HDR + depth to holodeck */
19     #define FROM_STDIN 3 /* copy from stdin to holodeck */
20     #define TO_STDOUT 4 /* copy rays from holodeck to stdout */
21 gregl 3.7
22 greg 3.34 int operation = 0; /* what we are doing */
23     char *rspec = ""; /* ray details for i/o */
24 gwlarson 3.14 int checkdepth = 1; /* check depth (!-d option)? */
25     int checkrepeats = 0; /* check for repeats (-u option)? */
26 greg 3.34 int nholosects; /* number of original holodeck sections */
27     int iofmt = 'a'; /* input/output format for rays */
28    
29     /* holodeck flags */
30     #define H_BADF 01 /* bad format */
31     #define H_OBST 02 /* OBSTRUCTIONS= True */
32     #define H_OBSF 04 /* OBSTRUCTIONS= False */
33     #define H_VDST 010 /* VDISTANCE= True */
34     #define H_SWAP 020 /* byte order is different */
35 gregl 3.1
36     char *progname; /* global argv[0] */
37    
38 schorsch 3.22 struct phead {
39     VIEW vw;
40     double expos;
41     short gotview;
42     short badfmt;
43     short altprims;
44     };
45 greg 3.34
46     typedef struct {
47     FVECT ro;
48     FVECT rd;
49     RREAL d;
50     COLR cv;
51     } RAYPAR;
52    
53 schorsch 3.22 static int openholo(char *fname, int append);
54 greg 3.34 static void addray(RAYPAR *rp);
55     static int readval(RREAL *v, int n, FILE *fp);
56     static void readrays(FILE *fp);
57     static int writeval(RREAL *v, int n, FILE *fp);
58     static int write_ray(RAYPAR *rp, FILE *fp);
59     static void writerays(FILE *fp);
60 schorsch 3.23 static gethfunc holheadline;
61 schorsch 3.22 static int bpcmp(const void *b1p, const void *b2p);
62     static int addclump(HOLO *hp, int *bq, int nb);
63     static void addholo(char *hdf);
64 schorsch 3.23 static gethfunc picheadline;
65 schorsch 3.22 static void addpicz(char *pcf, char *zbf);
66    
67    
68     int
69     main(
70     int argc,
71     char *argv[]
72     )
73 gregl 3.1 {
74     int i;
75    
76     progname = argv[0];
77 gregl 3.5 for (i = 2; i < argc && argv[i][0] == '-'; i++)
78     switch (argv[i][1]) {
79 gregl 3.8 case 'u':
80 gregl 3.5 checkrepeats = 1;
81     break;
82 gregl 3.8 case 'd':
83 gregl 3.5 checkdepth = 0;
84     break;
85 greg 3.34 case 'f':
86     iofmt = argv[i][2];
87     if (!strchr("afd", iofmt))
88     error(USER, "-f? i/o format must be 'a', 'f', or 'd'");
89     break;
90 gregl 3.5 case 'h':
91 greg 3.34 operation = FROM_HOLO;
92 gregl 3.5 break;
93     case 'p':
94 greg 3.34 operation = FROM_PICZ;
95     break;
96     case 'i':
97     operation = FROM_STDIN;
98     rspec = argv[i]+2;
99     break;
100     case 'o':
101     operation = TO_STDOUT;
102     rspec = argv[i]+2;
103 gregl 3.5 break;
104     default:
105     goto userr;
106     }
107 greg 3.34 if (!operation | (i > argc-((operation==FROM_HOLO)|(operation==FROM_PICZ))))
108 gregl 3.1 goto userr;
109 greg 3.34 if (operation == FROM_PICZ && (argc-i)%2)
110 gregl 3.1 goto userr;
111 greg 3.34 nholosects = openholo(argv[1], (operation != TO_STDOUT));
112     /* check requested i/o is compatible */
113     if (strchr(rspec, 'l') && !(*(int *)hdlist[0]->priv & H_VDST))
114     error(USER, "i/o parameter 'l' incompatible with VDISTANCE=False");
115     if (strchr(rspec, 'L') && *(int *)hdlist[0]->priv & H_VDST)
116     error(USER, "i/o parameter 'L' incompatible with VDISTANCE=True");
117    
118     switch (operation) { /* perform requested operation */
119     case FROM_PICZ:
120 gregl 3.5 for ( ; i < argc; i += 2)
121 gregl 3.1 addpicz(argv[i], argv[i+1]);
122 greg 3.34 break;
123     case FROM_HOLO:
124 gwlarson 3.10 if (BKBSIZE*1024*1.5 > hdcachesize)
125     hdcachesize = BKBSIZE*1024*1.5;
126 gregl 3.5 for ( ; i < argc; i++)
127 gregl 3.1 addholo(argv[i]);
128 greg 3.34 break;
129     case FROM_STDIN:
130     readrays(stdin);
131     break;
132     case TO_STDOUT:
133     writerays(stdout);
134     break;
135 gwlarson 3.10 }
136 gregl 3.1 quit(0);
137     userr:
138 greg 3.34 fprintf(stderr, "Usage: %s dest.hdk [-u][-d] -h inp1.hdk ..\n",
139 gregl 3.1 progname);
140 greg 3.34 fprintf(stderr, " Or: %s dest.hdk [-u][-d] -p inp1.hdr inp1.zbf ..\n",
141     progname);
142     fprintf(stderr, " Or: %s dest.hdk [-f{a|f|d}][-u][-d] -i[odplLv]\n",
143     progname);
144     fprintf(stderr, " Or: %s src.hdk [-f{a|f|d}] -o[odplLv] ..\n",
145 gregl 3.5 progname);
146 gregl 3.1 exit(1);
147     }
148    
149 schorsch 3.23 static int
150 schorsch 3.22 holheadline( /* check holodeck header line */
151 greg 3.31 char *s,
152 schorsch 3.23 void *vhf
153 schorsch 3.22 )
154 gregl 3.1 {
155 greg 3.33 int be;
156 greg 3.30 char fmt[MAXFMTLEN];
157 greg 3.34 int *hf = (int *)vhf;
158 gregl 3.1
159     if (formatval(fmt, s)) {
160 gregl 3.2 if (strcmp(fmt, HOLOFMT))
161     *hf |= H_BADF;
162     else
163     *hf &= ~H_BADF;
164 gwlarson 3.9 return(0);
165 gregl 3.1 }
166     if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
167     s += 13;
168     while (*s == ' ') s++;
169 schorsch 3.19 if ((*s == 't') | (*s == 'T'))
170 gregl 3.2 *hf |= H_OBST;
171 schorsch 3.19 else if ((*s == 'f') | (*s == 'F'))
172 gregl 3.2 *hf |= H_OBSF;
173 gregl 3.1 else
174     error(WARNING, "bad OBSTRUCTIONS value in holodeck");
175 gwlarson 3.9 return(0);
176 gregl 3.1 }
177 greg 3.34 if (!strncmp(s, "VDISTANCE=", 10)) {
178     s += 10;
179     while (*s == ' ') s++;
180     if ((*s == 't') | (*s == 'T'))
181     *hf |= H_VDST;
182     else if ((*s != 'f') % (*s != 'F'))
183     error(WARNING, "bad VDISTANCE value in holodeck");
184     return(0);
185     }
186 greg 3.33 if ((be = isbigendian(s)) >= 0) {
187     if (be != nativebigendian())
188     *hf |= H_SWAP;
189 greg 3.34 return(0);
190 greg 3.33 }
191 gwlarson 3.9 return(0);
192 gregl 3.1 }
193    
194     int
195 schorsch 3.22 openholo( /* open existing holodeck file for i/o */
196     char *fname,
197     int append
198     )
199 gregl 3.1 {
200     FILE *fp;
201     int fd;
202 gregl 3.2 int hflags = 0;
203 greg 3.34 int *hfstore;
204 greg 3.26 off_t nextloc;
205 gregl 3.1 int n;
206     /* open holodeck file */
207 greg 3.32 if ((fp = fopen(fname, append ? "rb+" : "rb")) == NULL) {
208 gregl 3.1 sprintf(errmsg, "cannot open \"%s\" for %s", fname,
209     append ? "appending" : "reading");
210     error(SYSTEM, errmsg);
211     }
212     /* check header and magic number */
213 schorsch 3.23 if (getheader(fp, holheadline, &hflags) < 0 ||
214 greg 3.33 hflags&(H_BADF|H_SWAP) || getw(fp) != HOLOMAGIC) {
215     sprintf(errmsg, "holodeck \"%s\" not in expected format", fname);
216 gregl 3.1 error(USER, errmsg);
217     }
218     fd = dup(fileno(fp)); /* dup file handle */
219     nextloc = ftell(fp); /* get stdio position */
220     fclose(fp); /* done with stdio */
221 greg 3.34 hfstore = (int *)malloc(sizeof(int)); /* tiny memory leak but who cares? */
222     *hfstore = hflags;
223 gregl 3.1 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
224 greg 3.26 lseek(fd, nextloc, SEEK_SET);
225 gregl 3.1 read(fd, (char *)&nextloc, sizeof(nextloc));
226 greg 3.34 hdinit(fd, NULL)->priv = hfstore;
227 gregl 3.1 }
228     return(n);
229     }
230    
231 schorsch 3.22 void
232     addray( /* add a ray to our output holodeck */
233 greg 3.34 RAYPAR *rp
234 schorsch 3.22 )
235 gregl 3.1 {
236 gregl 3.5 int sn, bi, n;
237 greg 3.31 HOLO *hp;
238 gregl 3.1 GCOORD gc[2];
239 greg 3.27 uby8 rr[2][2];
240 gregl 3.5 BEAM *bp;
241 gregl 3.1 double d0, d1;
242 gregl 3.5 unsigned dc;
243 greg 3.31 RAYVAL *rv;
244 gregl 3.1 /* check each output section */
245 greg 3.34 for (sn = nholosects; sn--; ) {
246 gregl 3.1 hp = hdlist[sn];
247 greg 3.34 d0 = hdinter(gc, rr, &d1, hp, rp->ro, rp->rd);
248     if (rp->d <= d0 || d1 < -0.001)
249 gregl 3.1 continue; /* missed section */
250 gregl 3.5 if (checkdepth) { /* check depth */
251 greg 3.34 if (*(int *)hp->priv & H_OBST && d0 < -0.001)
252 gregl 3.5 continue; /* ray starts too late */
253 greg 3.34 if (*(int *)hp->priv & H_OBSF && rp->d < 0.999*d1)
254 gregl 3.5 continue; /* ray ends too soon */
255     }
256 greg 3.34 dc = hdcode(hp, rp->d-d0);
257 gregl 3.5 bi = hdbindex(hp, gc); /* check for duplicates */
258     if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) {
259     for (n = bp->nrm, rv = hdbray(bp); n--; rv++)
260 greg 3.34 if ((rv->d == dc || *(int *)hp->priv & (H_OBST|H_OBSF)) &&
261 gregl 3.5 rv->r[0][0] == rr[0][0] &&
262     rv->r[0][1] == rr[0][1] &&
263     rv->r[1][0] == rr[1][0] &&
264     rv->r[1][1] == rr[1][1])
265     break;
266     if (n >= 0)
267     continue; /* found a matching ray */
268     }
269     rv = hdnewrays(hp, bi, 1);
270     rv->d = dc;
271 gregl 3.1 rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
272     rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
273 greg 3.34 copycolr(rv->v, rp->cv);
274     }
275     }
276    
277     /* Read n-vector from file stream */
278     static int
279     readval(RREAL *v, int n, FILE *fp)
280     {
281     int i;
282     #ifdef SMLFLT
283     double vd[3];
284     switch (iofmt) {
285     case 'f':
286     return getbinary(v, sizeof(float), n, fp);
287     case 'd':
288     n = getbinary(vd, sizeof(double), n, fp);
289     for (i = n; i-- > 0; ) v[i] = vd[i];
290     return n;
291     case 'a':
292     for (i = 0; i < n; i++)
293     if (fscanf(fp, "%f ", &v[i]) != 1)
294     break;
295     return i;
296     }
297     #else
298     float vf[3];
299     switch (iofmt) {
300     case 'd':
301     return getbinary(v, sizeof(double), n, fp);
302     case 'f':
303     n = getbinary(vf, sizeof(float), n, fp);
304     for (i = n; i-- > 0; ) v[i] = vf[i];
305     return n;
306     case 'a':
307     for (i = 0; i < n; i++)
308     if (fscanf(fp, "%lf ", &v[i]) != 1)
309     break;
310     return i;
311     }
312     #endif
313     return -1;
314     }
315    
316     #define GOT_ORG 0x01
317     #define GOT_DIR 0x02
318     #define GOT_LEN 0x04
319     #define GOT_VAL 0x10
320     #define ALSO_POS 0x20
321     #define BAD_DIR 0x40
322     #define BAD_LEN 0x80
323    
324     /* Read rays from stream and add to holodeck */
325     static void
326     readrays(FILE *fp)
327     {
328     if (iofmt != 'a')
329     SET_FILE_BINARY(fp);
330     #ifdef getc_unlocked
331     flockfile(fp);
332     #endif
333     while (!feof(fp)) { /* read entirety of input */
334     RAYPAR ryp;
335     FVECT pos;
336     FVECT col;
337     int flags = 0;
338     int i;
339     for (i = 0; rspec[i]; i++) {
340     switch (rspec[i]) {
341     case 'o': /* ray origin */
342     if (readval(ryp.ro, 3, fp) < 3)
343     break;
344     flags |= GOT_ORG;
345     continue;
346     case 'd': /* ray direction */
347     if (readval(ryp.rd, 3, fp) < 3)
348     break;
349     if (normalize(ryp.rd) == 0)
350     flags |= BAD_DIR;
351     else
352     flags |= GOT_DIR;
353     continue;
354     case 'p': /* ray intersection */
355     if (readval(pos, 3, fp) < 3)
356     break;
357     flags |= ALSO_POS;
358     continue;
359     case 'L': /* ray first length */
360     case 'l': /* ray virtual length */
361     if (readval(&ryp.d, 1, fp) < 1)
362     break;
363     if (ryp.d <= FTINY)
364     flags |= BAD_LEN;
365     else
366     flags |= GOT_LEN;
367     continue;
368     case 'v': /* ray value */
369     if (readval(col, 3, fp) < 3)
370     break;
371     setcolr(ryp.cv, col[0], col[1], col[2]);
372     flags |= GOT_VAL;
373     continue;
374     default:
375     sprintf(errmsg, "unsupported parameter '%c' in -i%s",
376     rspec[i], rspec);
377     error(USER, errmsg);
378     }
379     if (!flags) /* got nothing, so may be normal EOF */
380     return;
381     }
382     if (flags & (BAD_DIR|BAD_LEN))
383     continue; /* just a bad ray is all -- skip */
384     if (!(flags & GOT_VAL))
385     goto missingData;
386     if ((flags & (GOT_ORG|GOT_DIR|GOT_LEN)) != (GOT_ORG|GOT_DIR|GOT_LEN)) {
387     if (!(flags & ALSO_POS))
388     goto missingData;
389     if (flags & GOT_ORG) {
390     VSUB(ryp.rd, pos, ryp.ro);
391     ryp.d = normalize(ryp.rd);
392     if (ryp.d == 0)
393     continue;
394     } else if ((flags & (GOT_DIR|GOT_LEN)) == (GOT_DIR|GOT_LEN)) {
395     VSUM(ryp.ro, pos, ryp.rd, -ryp.d);
396     } else
397     goto missingData;
398     }
399     addray(&ryp); /* add our ray to holodeck */
400     }
401     return;
402     missingData:
403 greg 3.37 sprintf(errmsg, "insufficient data or read error for -i%s", rspec);
404 greg 3.34 error(USER, errmsg);
405     }
406    
407     /* Write vector value to file stream */
408     static int
409     writeval(RREAL *v, int n, FILE *fp)
410     {
411     int i;
412    
413     if (iofmt == 'a') {
414     for (i = 0; i < n; i++)
415     if (fprintf(fp, "\t%.4e", v[i]) < 0)
416     break;
417     return i;
418     }
419     #ifdef SMLFLT
420     if (iofmt == 'd') {
421     double vd[3];
422     for (i = n; i--; ) vd[i] = v[i];
423     return putbinary(vd, sizeof(double), n, fp);
424     }
425     #else
426     if (iofmt == 'f') {
427     float vf[3];
428     for (i = n; i--; ) vf[i] = v[i];
429     return putbinary(vf, sizeof(float), n, fp);
430 gregl 3.1 }
431 greg 3.34 #endif
432     return putbinary(v, sizeof(*v), n, fp);
433 gregl 3.1 }
434    
435 greg 3.34 /* Write out an individual ray as requested */
436     static int
437     write_ray(RAYPAR *rp, FILE *fp)
438     {
439     COLOR cval;
440     FVECT v3;
441     char *typ = rspec;
442    
443     for ( ; ; ) {
444     switch (*typ++) {
445     case 'o': /* ray origin */
446     if (writeval(rp->ro, 3, fp) < 3)
447     break;
448     continue;
449     case 'd': /* ray direction */
450     if (writeval(rp->rd, 3, fp) < 3)
451     break;
452     continue;
453     case 'p': /* ray intersection */
454     VSUM(v3, rp->ro, rp->rd, rp->d);
455     if (writeval(v3, 3, fp) < 3)
456     break;
457     continue;
458     case 'L': /* ray first length */
459     case 'l': /* ray virtual length */
460     if (writeval(&rp->d, 1, fp) < 1)
461     break;
462     continue;
463     case 'v': /* ray value */
464     colr_color(cval, rp->cv);
465     VCOPY(v3, cval);
466     if (writeval(v3, 3, fp) < 3)
467     break;
468     continue;
469     case '\0': /* end of spec -- success */
470     if (iofmt == 'a')
471     fputc('\n', fp);
472     return(1);
473     default:
474     sprintf(errmsg, "unsupported parameter '%c' in -o%s", typ[-1], rspec);
475     }
476     break; /* land here on error */
477     }
478     return 0; /* write error? */
479     }
480    
481 greg 3.35 static BEAMI *beamdir;
482    
483     static int
484     bpcmp( /* compare beam positions on disk */
485     const void *b1p,
486     const void *b2p
487     )
488     {
489     off_t pdif = beamdir[*(int *)b1p].fo - beamdir[*(int *)b2p].fo;
490    
491     if (pdif > 0L) return(1);
492     if (pdif < 0L) return(-1);
493     return(0);
494     }
495    
496 greg 3.34 /* Write all rays from holodeck to stream */
497     static void
498     writerays(FILE *fp)
499     {
500     int sn, bi, k;
501     GCOORD gc[2];
502     RAYVAL *rv;
503     RAYPAR ryp;
504    
505     if (!*rspec) {
506     error(WARNING, "empty -o* output spec, quitting");
507     return;
508     }
509     if (iofmt != 'a')
510     SET_FILE_BINARY(fp);
511     #ifdef getc_unlocked
512     flockfile(fp);
513     #endif
514     for (sn = 0; sn < nholosects; sn++) { /* write each holodeck section */
515     HOLO *hp = hdlist[sn];
516 greg 3.35 int nb = nbeams(hp); /* sort beams by file location */
517     int *bq = (int *)malloc(nb*sizeof(int));
518     if (!bq)
519     error(SYSTEM, "out of memory in writerays()");
520     for (bi = nb; bi--; ) bq[bi] = bi+1;
521     beamdir = hp->bi;
522     qsort(bq, nb, sizeof(*bq), bpcmp);
523     for (bi = 0; bi < nb; bi++) {
524     BEAM *bp = hdgetbeam(hp, bq[bi]);
525 greg 3.34 if (!bp) /* empty beam? */
526     continue;
527 greg 3.35 hdbcoord(gc, hp, bq[bi]);
528 greg 3.34 rv = hdbray(bp);
529     for (k = bp->nrm; k--; rv++) {
530     ryp.d = hdray(ryp.ro, ryp.rd, hp, gc, rv->r);
531     if (*(int *)hp->priv & H_OBSF)
532     VSUM(ryp.ro, ryp.ro, ryp.rd, ryp.d);
533     else
534     ryp.d = 0.;
535     ryp.d = hddepth(hp, rv->d) - ryp.d;
536     copycolr(ryp.cv, rv->v);
537 greg 3.35 if (!write_ray(&ryp, fp)) {
538     free(bq);
539 greg 3.34 goto writError;
540 greg 3.35 }
541 greg 3.34 }
542 greg 3.35 hdfreebeam(hp, bq[bi]);
543 greg 3.34 }
544 greg 3.35 free(bq);
545 greg 3.34 }
546     if (fflush(fp) != EOF)
547     return;
548     writError:
549     error(SYSTEM, "error writing holodeck rays");
550     }
551 gregl 3.1
552 gwlarson 3.11 static int
553 schorsch 3.22 addclump( /* transfer the given clump and free */
554     HOLO *hp,
555     int *bq,
556     int nb
557     )
558 gwlarson 3.11 {
559 gregl 3.1 GCOORD gc[2];
560 greg 3.34 RAYPAR ryp;
561     RAYVAL *rv;
562 gwlarson 3.11 int i;
563 greg 3.31 int k;
564     BEAM *bp;
565 gwlarson 3.11 /* sort based on file position */
566     beamdir = hp->bi;
567 greg 3.35 qsort(bq, nb, sizeof(*bq), bpcmp);
568 gwlarson 3.11 /* transfer each beam */
569     for (i = 0; i < nb; i++) {
570     bp = hdgetbeam(hp, bq[i]);
571     hdbcoord(gc, hp, bq[i]);
572 greg 3.34 rv = hdbray(bp); /* add each ray to output */
573     for (k = bp->nrm; k--; rv++) {
574     ryp.d = hdray(ryp.ro, ryp.rd, hp, gc, rv->r);
575     if (*(int *)hp->priv & H_OBSF)
576     VSUM(ryp.ro, ryp.ro, ryp.rd, ryp.d);
577 gwlarson 3.11 else
578 greg 3.34 ryp.d = 0.;
579     ryp.d = hddepth(hp, rv->d) - ryp.d;
580     copycolr(ryp.cv, rv->v);
581     addray(&ryp);
582 gwlarson 3.11 }
583     hdfreebeam(hp, bq[i]); /* free the beam */
584 gwlarson 3.10 }
585 gwlarson 3.11 return(0);
586 gwlarson 3.10 }
587    
588 schorsch 3.22
589     void
590     addholo( /* add a holodeck file */
591     char *hdf
592     )
593 gwlarson 3.10 {
594     int fd;
595 gregl 3.4 /* open the holodeck for reading */
596     openholo(hdf, 0);
597 greg 3.34 fd = hdlist[nholosects]->fd; /* remember the file handle */
598     while (hdlist[nholosects] != NULL) { /* load each section */
599 gwlarson 3.10 /* clump the beams */
600 greg 3.34 clumpbeams(hdlist[nholosects], 0, BKBSIZE*1024, addclump);
601     hddone(hdlist[nholosects]); /* free the section */
602 gregl 3.1 }
603 gwlarson 3.10 close(fd); /* close input file */
604 gwlarson 3.13 hdflush(NULL); /* flush output */
605 gregl 3.1 }
606    
607    
608    
609 schorsch 3.23 static int
610 schorsch 3.22 picheadline( /* process picture header line */
611     char *s,
612 schorsch 3.23 void *vph
613 schorsch 3.22 )
614 gregl 3.1 {
615     char fmt[32];
616 greg 3.34 struct phead *ph = (struct phead *)vph;
617 gregl 3.1
618     if (formatval(fmt, s)) {
619     ph->badfmt = strcmp(fmt, COLRFMT);
620 gwlarson 3.9 return(0);
621 gregl 3.1 }
622     if (isprims(s)) {
623     ph->altprims++; /* don't want to deal with this */
624 gwlarson 3.9 return(0);
625 gregl 3.1 }
626     if (isexpos(s)) {
627     ph->expos *= exposval(s);
628 gwlarson 3.9 return(0);
629 gregl 3.1 }
630     if (isview(s)) {
631     ph->gotview += sscanview(&ph->vw, s);
632 gwlarson 3.9 return(0);
633 gregl 3.1 }
634 gwlarson 3.9 return(0);
635 gregl 3.1 }
636    
637    
638 schorsch 3.22 void
639     addpicz( /* add a picture + depth-buffer */
640     char *pcf,
641     char *zbf
642     )
643 gregl 3.1 {
644     FILE *pfp;
645     int zfd;
646     COLR *cscn;
647     float *zscn;
648     struct phead phd;
649     int eshft;
650     double emult;
651     RESOLU prs;
652 schorsch 3.17 RREAL vl[2];
653 greg 3.34 RAYPAR ryp;
654 gregl 3.1 double aftd;
655 greg 3.34 int j, i;
656     /* open picture & get header */
657 greg 3.32 if ((pfp = fopen(pcf, "rb")) == NULL) {
658 gregl 3.1 sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
659     error(SYSTEM, pcf);
660     }
661 schorsch 3.18 phd.vw = stdview;
662 gregl 3.1 phd.expos = 1.0;
663     phd.badfmt = phd.gotview = phd.altprims = 0;
664 schorsch 3.23 if (getheader(pfp, picheadline, &phd) < 0 ||
665 gregl 3.1 phd.badfmt || !fgetsresolu(&prs, pfp)) {
666     sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
667     error(USER, errmsg);
668     }
669     if (!phd.gotview || setview(&phd.vw) != NULL) {
670     sprintf(errmsg, "missing/illegal view in picture \"%s\"",
671     pcf);
672     error(USER, errmsg);
673     }
674     if (phd.altprims) {
675 greg 3.28 sprintf(errmsg, "ignoring color primaries in picture \"%s\"",
676 gregl 3.1 pcf);
677     error(WARNING, errmsg);
678     }
679 greg 3.34 /* open depth buffer */
680     if ((zfd = open_float_depth(zbf, prs.xr*prs.yr)) < 0)
681     quit(1);
682 gregl 3.1 /* figure out what to do about exposure */
683 schorsch 3.19 if ((phd.expos < 0.99) | (phd.expos > 1.01)) {
684 gregl 3.1 emult = -log(phd.expos)/log(2.);
685     eshft = emult >= 0. ? emult+.5 : emult-.5;
686     emult -= (double)eshft;
687 schorsch 3.19 if ((emult <= 0.01) & (emult >= -0.01))
688 gregl 3.1 emult = -1.;
689     else {
690     emult = 1./phd.expos;
691     eshft = 0;
692     }
693     } else {
694     emult = -1.;
695     eshft = 0;
696     }
697     /* allocate buffers */
698     cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
699     zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
700 schorsch 3.19 if ((cscn == NULL) | (zscn == NULL))
701 gregl 3.1 error(SYSTEM, "out of memory in addpicz");
702     /* read and process each scanline */
703     for (j = 0; j < numscans(&prs); j++) {
704     i = scanlen(&prs); /* read colrs */
705     if (freadcolrs(cscn, i, pfp) < 0) {
706     sprintf(errmsg, "error reading picture \"%s\"", pcf);
707     error(USER, errmsg);
708     }
709     if (eshft) /* shift exposure */
710     shiftcolrs(cscn, i, eshft);
711 greg 3.32 /* read depth */
712     if (read(zfd, zscn, i*sizeof(float)) != i*sizeof(float)) {
713 gregl 3.1 sprintf(errmsg, "error reading depth file \"%s\"", zbf);
714     error(USER, errmsg);
715     }
716 greg 3.32 while (i--) { /* process each pixel */
717 greg 3.29 if (zscn[i] <= 0.0)
718     continue; /* illegal depth */
719 gregl 3.1 pix2loc(vl, &prs, i, j);
720 greg 3.34 aftd = viewray(ryp.ro, ryp.rd, &phd.vw, vl[0], vl[1]);
721 gregl 3.1 if (aftd < -FTINY)
722     continue; /* off view */
723     if (aftd > FTINY && zscn[i] > aftd)
724     continue; /* aft clipped */
725 greg 3.34 ryp.d = (RREAL)zscn[i];
726     copycolr(ryp.cv, cscn[i]);
727 gregl 3.1 if (emult > 0.) { /* whatta pain */
728 greg 3.34 COLOR ctmp;
729     colr_color(ctmp, ryp.cv);
730 gregl 3.1 scalecolor(ctmp, emult);
731 greg 3.34 setcolr(ryp.cv, colval(ctmp,RED),
732 gregl 3.1 colval(ctmp,GRN), colval(ctmp,BLU));
733     }
734 greg 3.34 addray(&ryp);
735 gregl 3.1 }
736     }
737 gwlarson 3.11 /* write output and free beams */
738     hdflush(NULL);
739 gregl 3.1 /* clean up */
740 greg 3.15 free((void *)cscn);
741     free((void *)zscn);
742 gregl 3.1 fclose(pfp);
743     close(zfd);
744     }
745    
746    
747 greg 3.15 void
748 schorsch 3.22 eputs( /* put error message to stderr */
749 greg 3.36 const char *s
750 schorsch 3.22 )
751 gregl 3.1 {
752     static int midline = 0;
753    
754     if (!*s)
755     return;
756     if (!midline++) { /* prepend line with program name */
757     fputs(progname, stderr);
758     fputs(": ", stderr);
759     }
760     fputs(s, stderr);
761     if (s[strlen(s)-1] == '\n') {
762     fflush(stderr);
763     midline = 0;
764     }
765     }
766    
767    
768 greg 3.15 void
769 schorsch 3.22 quit( /* exit the program gracefully */
770     int code
771     )
772 gregl 3.1 {
773     hdsync(NULL, 1); /* write out any buffered data */
774     exit(code);
775     }