ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
(Generate patch)

Comparing ray/src/hd/rhcopy.c (file contents):
Revision 3.4 by gregl, Tue Jan 6 22:02:22 1998 UTC vs.
Revision 3.22 by schorsch, Thu Jan 1 11:21:55 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
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"
13 #include "resolu.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 < main(argc, argv)
42 < int     argc;
43 < char    *argv[];
41 >
42 >
43 > int
44 > main(
45 >        int     argc,
46 >        char    *argv[]
47 > )
48   {
49          int     i;
50  
51          progname = argv[0];
52 <        if (argc < 4)
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 (!strcmp(argv[2], "-h"))
32 <                frompicz = 0;
33 <        else if (!strcmp(argv[2], "-pz"))
34 <                frompicz = 1;
35 <        else
72 >        if (frompicz && (argc-i)%2)
73                  goto userr;
37        if (frompicz && (argc-3)%2)
38                goto userr;
74          noutsects = openholo(argv[1], 1);
75 <        if (frompicz)
76 <                for (i = 3; i < argc; i += 2)
75 >        if (frompicz) {
76 >                for ( ; i < argc; i += 2)
77                          addpicz(argv[i], argv[i+1]);
78 <        else
79 <                for (i = 3; i < argc; i++)
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 -h inp1.hdk ..\n", progname);
49 <        fprintf(stderr, "   Or: %s output.hdk -pz inp1.pic inp1.zbf ..\n",
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  
# Line 56 | Line 95 | userr:
95   #define H_OBST  02
96   #define H_OBSF  04
97  
98 < holheadline(s, hf)              /* check holodeck header line */
99 < register char   *s;
100 < int     *hf;
98 > int
99 > holheadline(            /* check holodeck header line */
100 >        register char   *s,
101 >        int     *hf
102 > )
103   {
104          char    fmt[32];
105  
# Line 67 | Line 108 | int    *hf;
108                          *hf |= H_BADF;
109                  else
110                          *hf &= ~H_BADF;
111 <                return;
111 >                return(0);
112          }
113          if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
114                  s += 13;
115                  while (*s == ' ') s++;
116 <                if (*s == 't' | *s == 'T')
116 >                if ((*s == 't') | (*s == 'T'))
117                          *hf |= H_OBST;
118 <                else if (*s == 'f' | *s == 'F')
118 >                else if ((*s == 'f') | (*s == 'F'))
119                          *hf |= H_OBSF;
120                  else
121                          error(WARNING, "bad OBSTRUCTIONS value in holodeck");
122 <                return;
122 >                return(0);
123          }
124 +        return(0);
125   }
126  
127   int
128 < openholo(fname, append)         /* open existing holodeck file for i/o */
129 < char    *fname;
130 < int     append;
128 > openholo(               /* open existing holodeck file for i/o */
129 >        char    *fname,
130 >        int     append
131 > )
132   {
90        extern long     ftell();
133          FILE    *fp;
134          int     fd;
135          int     hflags = 0;
# Line 100 | Line 142 | int    append;
142                  error(SYSTEM, errmsg);
143          }
144                                          /* check header and magic number */
145 <        if (getheader(fp, holheadline, &hflags) < 0 ||
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);
# Line 109 | Line 151 | int    append;
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, nextloc, 0);
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;
# Line 122 | Line 164 | int    append;
164   #undef H_OBSF
165  
166  
167 < addray(ro, rd, d, cv)           /* add a ray to our output holodeck */
168 < FVECT   ro, rd;
169 < double  d;
170 < COLR    cv;
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;
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--; ) {
# Line 139 | Line 186 | COLR   cv;
186                  d0 = hdinter(gc, rr, &d1, hp, ro, rd);
187                  if (d <= d0 || d1 < -0.001)
188                          continue;       /* missed section */
189 <                if (hp->priv == &obstr && d0 < -0.001)
190 <                        continue;       /* ray starts too late */
191 <                if (hp->priv == &unobstr && d < 0.999*d1)
192 <                        continue;       /* ray ends too soon */
193 <                                        /* should we check for duplicates? */
194 <                rv = hdnewrays(hp, hdbindex(hp, gc), 1);
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);
151                rv->d = hdcode(hp, d-d0);
213          }
214   }
215  
216  
217 < addholo(hdf)                    /* add a holodeck file */
218 < char    *hdf;
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 <        int     fd;
226 <        register HOLO   *hp;
227 <        register BEAM   *bp;
228 <        register HDBEAMI        *hbl;
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, j;
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 ((hp = hdlist[noutsects]) != NULL) {      /* load each section */
278 <                hbl = (HDBEAMI *)malloc(nbeams(hp)*sizeof(HDBEAMI));
279 <                if (hbl == NULL)
280 <                        error(SYSTEM, "out of memory in addholo");
175 <                for (j = nbeams(hp); j > 0; j--) {      /* sort the beams */
176 <                        hbl[j].h = hp;
177 <                        hbl[j].b = j;
178 <                }
179 <                qsort((char *)hbl, nbeams(hp), sizeof(HDBEAMI), hdfilord);
180 <                for (j = 0; j < nbeams(hp); j++)        /* load each beam */
181 <                        if ((bp = hdgetbeam(hp, hbl[j].b)) != NULL) {
182 <                                hdbcoord(gc, hp, hbl[j].b);
183 <                                for (k = bp->nrm; k--; ) {
184 <                                        d = hdray(ro, rd,
185 <                                                hp, gc, hdbray(bp)[k].r);
186 <                                        if (hp->priv == &unobstr)
187 <                                                VSUM(ro, ro, rd, d);
188 <                                        else
189 <                                                d = 0.;
190 <                                        d = hddepth(hp, hdbray(bp)[k].d) - d;
191 <                                        addray(ro, rd, d, hdbray(bp)[k].v);
192 <                                }
193 <                                hdfreebeam(hp, hbl[j].b);       /* free beam */
194 <                        }
195 <                free((char *)hbl);                      /* free beam list */
196 <                hddone(hp);                             /* free the section */
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 the file */
282 >        close(fd);                      /* close input file */
283 >        hdflush(NULL);                  /* flush output */
284   }
285  
286  
202 struct phead {
203        VIEW    vw;
204        double  expos;
205        short   gotview;
206        short   badfmt;
207        short   altprims;
208 };
287  
288 <
289 < picheadline(s, ph)              /* process picture header line */
290 < char    *s;
291 < struct phead    *ph;
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;
298 >                return(0);
299          }
300          if (isprims(s)) {
301                  ph->altprims++;         /* don't want to deal with this */
302 <                return;
302 >                return(0);
303          }
304          if (isexpos(s)) {
305                  ph->expos *= exposval(s);
306 <                return;
306 >                return(0);
307          }
308          if (isview(s)) {
309                  ph->gotview += sscanview(&ph->vw, s);
310 <                return;
310 >                return(0);
311          }
312 +        return(0);
313   }
314  
315  
316 < addpicz(pcf, zbf)               /* add a picture + depth-buffer */
317 < char    *pcf, *zbf;
316 > void
317 > addpicz(                /* add a picture + depth-buffer */
318 >        char    *pcf,
319 >        char    *zbf
320 > )
321   {
322          FILE    *pfp;
323          int     zfd;
# Line 244 | Line 327 | char   *pcf, *zbf;
327          int     eshft;
328          double  emult;
329          RESOLU  prs;
330 <        FLOAT   vl[2];
330 >        RREAL   vl[2];
331          FVECT   ro, rd;
332          double  aftd;
333          COLOR   ctmp;
# Line 260 | Line 343 | char   *pcf, *zbf;
343                  error(SYSTEM, pcf);
344          }
345                                  /* load picture header */
346 <        copystruct(&phd.vw, &stdview);
346 >        phd.vw = stdview;
347          phd.expos = 1.0;
348          phd.badfmt = phd.gotview = phd.altprims = 0;
349 <        if (getheader(pfp, picheadline, &phd) < 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);
# Line 279 | Line 362 | char   *pcf, *zbf;
362                  error(WARNING, errmsg);
363          }
364                                  /* figure out what to do about exposure */
365 <        if (phd.expos < 0.99 | phd.expos > 1.01) {
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)
369 >                if ((emult <= 0.01) & (emult >= -0.01))
370                          emult = -1.;
371                  else {
372                          emult = 1./phd.expos;
# Line 296 | Line 379 | char   *pcf, *zbf;
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)
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++) {
# Line 328 | Line 411 | char   *pcf, *zbf;
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((char *)cscn);
418 <        free((char *)zscn);
417 >        free((void *)cscn);
418 >        free((void *)zscn);
419          fclose(pfp);
420          close(zfd);
421   }
422  
423  
424 < eputs(s)                        /* put error message to stderr */
425 < register char  *s;
424 > void
425 > eputs(                  /* put error message to stderr */
426 >        register char  *s
427 > )
428   {
429          static int  midline = 0;
430  
# Line 355 | Line 442 | register char  *s;
442   }
443  
444  
445 < quit(code)                      /* exit the program gracefully */
446 < int     code;
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines