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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rhcopy.c,v 3.21 2003/10/22 02:06:34 greg Exp $";
3 #endif
4 /*
5 * Copy data into a holodeck file
6 */
7
8 #include "platform.h"
9 #include "rterror.h"
10 #include "holo.h"
11 #include "view.h"
12
13 #ifndef BKBSIZE
14 #define BKBSIZE 256 /* beam clump size (kilobytes) */
15 #endif
16
17 int checkdepth = 1; /* check depth (!-d option)? */
18 int checkrepeats = 0; /* check for repeats (-u option)? */
19 int frompicz; /* input from pictures & depth-buffers? */
20 int noutsects; /* number of output sections */
21 char obstr, unobstr; /* flag pointer values */
22
23 char *progname; /* global argv[0] */
24
25 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
43 int
44 main(
45 int argc,
46 char *argv[]
47 )
48 {
49 int i;
50
51 progname = argv[0];
52 frompicz = -1;
53 for (i = 2; i < argc && argv[i][0] == '-'; i++)
54 switch (argv[i][1]) {
55 case 'u':
56 checkrepeats = 1;
57 break;
58 case 'd':
59 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 goto userr;
72 if (frompicz && (argc-i)%2)
73 goto userr;
74 noutsects = openholo(argv[1], 1);
75 if (frompicz) {
76 for ( ; i < argc; i += 2)
77 addpicz(argv[i], argv[i+1]);
78 } else {
79 if (BKBSIZE*1024*1.5 > hdcachesize)
80 hdcachesize = BKBSIZE*1024*1.5;
81 for ( ; i < argc; i++)
82 addholo(argv[i]);
83 }
84 quit(0);
85 userr:
86 fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n",
87 progname);
88 fprintf(stderr, " Or: %s output.hdk [-u][-d] -p inp1.pic inp1.zbf ..\n",
89 progname);
90 exit(1);
91 }
92
93
94 #define H_BADF 01
95 #define H_OBST 02
96 #define H_OBSF 04
97
98 int
99 holheadline( /* check holodeck header line */
100 register char *s,
101 int *hf
102 )
103 {
104 char fmt[32];
105
106 if (formatval(fmt, s)) {
107 if (strcmp(fmt, HOLOFMT))
108 *hf |= H_BADF;
109 else
110 *hf &= ~H_BADF;
111 return(0);
112 }
113 if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
114 s += 13;
115 while (*s == ' ') s++;
116 if ((*s == 't') | (*s == 'T'))
117 *hf |= H_OBST;
118 else if ((*s == 'f') | (*s == 'F'))
119 *hf |= H_OBSF;
120 else
121 error(WARNING, "bad OBSTRUCTIONS value in holodeck");
122 return(0);
123 }
124 return(0);
125 }
126
127 int
128 openholo( /* open existing holodeck file for i/o */
129 char *fname,
130 int append
131 )
132 {
133 FILE *fp;
134 int fd;
135 int hflags = 0;
136 long nextloc;
137 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 if (getheader(fp, holheadline, (char *)&hflags) < 0 ||
146 hflags&H_BADF || getw(fp) != HOLOMAGIC) {
147 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 lseek(fd, (off_t)nextloc, SEEK_SET);
155 read(fd, (char *)&nextloc, sizeof(nextloc));
156 hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
157 hflags&H_OBSF ? &unobstr : (char *)NULL;
158 }
159 return(n);
160 }
161
162 #undef H_BADF
163 #undef H_OBST
164 #undef H_OBSF
165
166
167 void
168 addray( /* add a ray to our output holodeck */
169 FVECT ro,
170 FVECT rd,
171 double d,
172 COLR cv
173 )
174 {
175 int sn, bi, n;
176 register HOLO *hp;
177 GCOORD gc[2];
178 BYTE rr[2][2];
179 BEAM *bp;
180 double d0, d1;
181 unsigned dc;
182 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 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 if ((hp->priv != NULL || rv->d == dc) &&
200 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 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 static BEAMI *beamdir;
218
219 static int
220 bpcmp( /* compare beam positions on disk */
221 const void *b1p,
222 const void *b2p
223 )
224 {
225 register off_t pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo;
226
227 if (pdif > 0L) return(1);
228 if (pdif < 0L) return(-1);
229 return(0);
230 }
231
232 static int
233 addclump( /* transfer the given clump and free */
234 HOLO *hp,
235 int *bq,
236 int nb
237 )
238 {
239 GCOORD gc[2];
240 FVECT ro, rd;
241 double d;
242 int i;
243 register int k;
244 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 }
264 return(0);
265 }
266
267
268 void
269 addholo( /* add a holodeck file */
270 char *hdf
271 )
272 {
273 int fd;
274 /* open the holodeck for reading */
275 openholo(hdf, 0);
276 fd = hdlist[noutsects]->fd; /* remember the file handle */
277 while (hdlist[noutsects] != NULL) { /* load each section */
278 /* clump the beams */
279 clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
280 hddone(hdlist[noutsects]); /* free the section */
281 }
282 close(fd); /* close input file */
283 hdflush(NULL); /* flush output */
284 }
285
286
287
288 int
289 picheadline( /* process picture header line */
290 char *s,
291 struct phead *ph
292 )
293 {
294 char fmt[32];
295
296 if (formatval(fmt, s)) {
297 ph->badfmt = strcmp(fmt, COLRFMT);
298 return(0);
299 }
300 if (isprims(s)) {
301 ph->altprims++; /* don't want to deal with this */
302 return(0);
303 }
304 if (isexpos(s)) {
305 ph->expos *= exposval(s);
306 return(0);
307 }
308 if (isview(s)) {
309 ph->gotview += sscanview(&ph->vw, s);
310 return(0);
311 }
312 return(0);
313 }
314
315
316 void
317 addpicz( /* add a picture + depth-buffer */
318 char *pcf,
319 char *zbf
320 )
321 {
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 RREAL vl[2];
331 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 phd.vw = stdview;
347 phd.expos = 1.0;
348 phd.badfmt = phd.gotview = phd.altprims = 0;
349 if (getheader(pfp, picheadline, (char *)&phd) < 0 ||
350 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 if ((phd.expos < 0.99) | (phd.expos > 1.01)) {
366 emult = -log(phd.expos)/log(2.);
367 eshft = emult >= 0. ? emult+.5 : emult-.5;
368 emult -= (double)eshft;
369 if ((emult <= 0.01) & (emult >= -0.01))
370 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 if ((cscn == NULL) | (zscn == NULL))
383 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 /* write output and free beams */
415 hdflush(NULL);
416 /* clean up */
417 free((void *)cscn);
418 free((void *)zscn);
419 fclose(pfp);
420 close(zfd);
421 }
422
423
424 void
425 eputs( /* put error message to stderr */
426 register char *s
427 )
428 {
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 void
446 quit( /* exit the program gracefully */
447 int code
448 )
449 {
450 hdsync(NULL, 1); /* write out any buffered data */
451 exit(code);
452 }