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.8 by gregl, Fri Jan 23 09:05:14 1998 UTC vs.
Revision 3.33 by greg, Thu Nov 7 23:17:58 2019 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 BLOADSIZE
14 < #define BLOADSIZE       1024    /* number of input beams to load at a time */
13 > #ifndef BKBSIZE
14 > #define BKBSIZE         256             /* beam clump size (kilobytes) */
15   #endif
16  
17 < int     checkdepth = 1;         /* check depth (!-f option)? */
18 < int     checkrepeats = 0;       /* check for repeats (-c option)? */
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 gethfunc holheadline;
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 gethfunc picheadline;
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  
# Line 55 | Line 72 | char   *argv[];
72          if (frompicz && (argc-i)%2)
73                  goto userr;
74          noutsects = openholo(argv[1], 1);
75 <        if (frompicz)
75 >        if (frompicz) {
76                  for ( ; i < argc; i += 2)
77                          addpicz(argv[i], argv[i+1]);
78 <        else
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] -pz inp1.pic inp1.zbf ..\n",
88 >        fprintf(stderr, "   Or: %s output.hdk [-u][-d] -p inp1.hdr inp1.zbf ..\n",
89                          progname);
90          exit(1);
91   }
# Line 74 | Line 94 | userr:
94   #define H_BADF  01
95   #define H_OBST  02
96   #define H_OBSF  04
97 + #define H_SWAP  010
98  
99 < holheadline(s, hf)              /* check holodeck header line */
100 < register char   *s;
101 < int     *hf;
99 > static int
100 > holheadline(            /* check holodeck header line */
101 >        char    *s,
102 >        void    *vhf
103 > )
104   {
105 <        char    fmt[32];
105 >        int     be;
106 >        char    fmt[MAXFMTLEN];
107 >        int     *hf = vhf;
108  
109          if (formatval(fmt, s)) {
110                  if (strcmp(fmt, HOLOFMT))
111                          *hf |= H_BADF;
112                  else
113                          *hf &= ~H_BADF;
114 <                return;
114 >                return(0);
115          }
116          if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
117                  s += 13;
118                  while (*s == ' ') s++;
119 <                if (*s == 't' | *s == 'T')
119 >                if ((*s == 't') | (*s == 'T'))
120                          *hf |= H_OBST;
121 <                else if (*s == 'f' | *s == 'F')
121 >                else if ((*s == 'f') | (*s == 'F'))
122                          *hf |= H_OBSF;
123                  else
124                          error(WARNING, "bad OBSTRUCTIONS value in holodeck");
125 <                return;
125 >                return(0);
126          }
127 +        if ((be = isbigendian(s)) >= 0) {
128 +                if (be != nativebigendian())
129 +                        *hf |= H_SWAP;
130 +        }
131 +        return(0);
132   }
133  
134   int
135 < openholo(fname, append)         /* open existing holodeck file for i/o */
136 < char    *fname;
137 < int     append;
135 > openholo(               /* open existing holodeck file for i/o */
136 >        char    *fname,
137 >        int     append
138 > )
139   {
109        extern long     ftell();
140          FILE    *fp;
141          int     fd;
142          int     hflags = 0;
143 <        long    nextloc;
143 >        off_t   nextloc;
144          int     n;
145                                          /* open holodeck file */
146 <        if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) {
146 >        if ((fp = fopen(fname, append ? "rb+" : "rb")) == NULL) {
147                  sprintf(errmsg, "cannot open \"%s\" for %s", fname,
148                                  append ? "appending" : "reading");
149                  error(SYSTEM, errmsg);
150          }
151                                          /* check header and magic number */
152          if (getheader(fp, holheadline, &hflags) < 0 ||
153 <                        hflags&H_BADF || getw(fp) != HOLOMAGIC) {
154 <                sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
153 >                        hflags&(H_BADF|H_SWAP) || getw(fp) != HOLOMAGIC) {
154 >                sprintf(errmsg, "holodeck \"%s\" not in expected format", fname);
155                  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 <                lseek(fd, nextloc, 0);
161 >                lseek(fd, nextloc, SEEK_SET);
162                  read(fd, (char *)&nextloc, sizeof(nextloc));
163                  hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
164                                  hflags&H_OBSF ? &unobstr : (char *)NULL;
# Line 141 | Line 171 | int    append;
171   #undef H_OBSF
172  
173  
174 < addray(ro, rd, d, cv)           /* add a ray to our output holodeck */
175 < FVECT   ro, rd;
176 < double  d;
177 < COLR    cv;
174 > void
175 > addray(         /* add a ray to our output holodeck */
176 >        FVECT   ro,
177 >        FVECT   rd,
178 >        double  d,
179 >        COLR    cv
180 > )
181   {
182          int     sn, bi, n;
183 <        register HOLO   *hp;
183 >        HOLO    *hp;
184          GCOORD  gc[2];
185 <        BYTE    rr[2][2];
185 >        uby8    rr[2][2];
186          BEAM    *bp;
187          double  d0, d1;
188          unsigned        dc;
189 <        register RAYVAL *rv;
189 >        RAYVAL  *rv;
190                                  /* check each output section */
191          for (sn = noutsects; sn--; ) {
192                  hp = hdlist[sn];
# Line 170 | Line 203 | COLR   cv;
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 <                                if (rv->d == dc &&
206 >                                if ((hp->priv != NULL || rv->d == dc) &&
207                                                  rv->r[0][0] == rr[0][0] &&
208                                                  rv->r[0][1] == rr[0][1] &&
209                                                  rv->r[1][0] == rr[1][0] &&
# Line 188 | Line 221 | COLR   cv;
221   }
222  
223  
224 < addholo(hdf)                    /* add a holodeck file */
225 < char    *hdf;
224 > static BEAMI    *beamdir;
225 >
226 > static int
227 > bpcmp(                  /* compare beam positions on disk */
228 >        const void      *b1p,
229 >        const void      *b2p
230 > )
231   {
232 <        HDBEAMI hbl[BLOADSIZE];
233 <        int     fd;
234 <        register HOLO   *hp;
235 <        register BEAM   *bp;
232 >        off_t   pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo;
233 >
234 >        if (pdif > 0L) return(1);
235 >        if (pdif < 0L) return(-1);
236 >        return(0);
237 > }
238 >
239 > static int
240 > addclump(               /* transfer the given clump and free */
241 >        HOLO    *hp,
242 >        int     *bq,
243 >        int     nb
244 > )
245 > {
246          GCOORD  gc[2];
247          FVECT   ro, rd;
248          double  d;
249 <        int     i, j, n, li;
250 <        register int    k;
249 >        int     i;
250 >        int     k;
251 >        BEAM    *bp;
252 >                                        /* 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 >        }
271 >        return(0);
272 > }
273 >
274 >
275 > void
276 > addholo(                        /* add a holodeck file */
277 >        char    *hdf
278 > )
279 > {
280 >        int     fd;
281                                          /* open the holodeck for reading */
282          openholo(hdf, 0);
283          fd = hdlist[noutsects]->fd;     /* remember the file handle */
284 <        while ((hp = hdlist[noutsects]) != NULL) {      /* load each section */
285 <                for (j = 0; j < nbeams(hp); j++) {      /* load each beam */
286 <                        if (!(li = j % BLOADSIZE)) {    /* optimize order */
287 <                                if (j+BLOADSIZE > nbeams(hp))
210 <                                        k = n = nbeams(hp) - j;
211 <                                else
212 <                                        k = n = BLOADSIZE;
213 <                                while (k--) {
214 <                                        hbl[k].h = hp;
215 <                                        hbl[k].b = j+k+1;
216 <                                }
217 <                                qsort((char *)hbl, n,
218 <                                                sizeof(HDBEAMI), hdfilord);
219 <                        }
220 <                        if ((bp = hdgetbeam(hp, hbl[li].b)) != NULL) {
221 <                                hdbcoord(gc, hp, hbl[li].b);
222 <                                for (k = bp->nrm; k--; ) {
223 <                                        d = hdray(ro, rd,
224 <                                                hp, gc, hdbray(bp)[k].r);
225 <                                        if (hp->priv == &unobstr)
226 <                                                VSUM(ro, ro, rd, d);
227 <                                        else
228 <                                                d = 0.;
229 <                                        d = hddepth(hp, hdbray(bp)[k].d) - d;
230 <                                        addray(ro, rd, d, hdbray(bp)[k].v);
231 <                                }
232 <                                hdfreebeam(hp, hbl[li].b);      /* free beam */
233 <                        }
234 <                }
235 <                hddone(hp);                             /* free the section */
284 >        while (hdlist[noutsects] != NULL) {     /* load each section */
285 >                                                        /* clump the beams */
286 >                clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
287 >                hddone(hdlist[noutsects]);              /* free the section */
288          }
289 <        close(fd);                      /* close the file */
289 >        close(fd);                      /* close input file */
290 >        hdflush(NULL);                  /* flush output */
291   }
292  
293  
241 struct phead {
242        VIEW    vw;
243        double  expos;
244        short   gotview;
245        short   badfmt;
246        short   altprims;
247 };
294  
295 <
296 < picheadline(s, ph)              /* process picture header line */
297 < char    *s;
298 < struct phead    *ph;
295 > static int
296 > picheadline(            /* process picture header line */
297 >        char    *s,
298 >        void    *vph
299 > )
300   {
301          char    fmt[32];
302 +        struct phead *ph = vph;
303  
304          if (formatval(fmt, s)) {
305                  ph->badfmt = strcmp(fmt, COLRFMT);
306 <                return;
306 >                return(0);
307          }
308          if (isprims(s)) {
309                  ph->altprims++;         /* don't want to deal with this */
310 <                return;
310 >                return(0);
311          }
312          if (isexpos(s)) {
313                  ph->expos *= exposval(s);
314 <                return;
314 >                return(0);
315          }
316          if (isview(s)) {
317                  ph->gotview += sscanview(&ph->vw, s);
318 <                return;
318 >                return(0);
319          }
320 +        return(0);
321   }
322  
323  
324 < addpicz(pcf, zbf)               /* add a picture + depth-buffer */
325 < char    *pcf, *zbf;
324 > void
325 > addpicz(                /* add a picture + depth-buffer */
326 >        char    *pcf,
327 >        char    *zbf
328 > )
329   {
330          FILE    *pfp;
331          int     zfd;
# Line 283 | Line 335 | char   *pcf, *zbf;
335          int     eshft;
336          double  emult;
337          RESOLU  prs;
338 <        FLOAT   vl[2];
338 >        RREAL   vl[2];
339          FVECT   ro, rd;
340          double  aftd;
341          COLOR   ctmp;
342          int     j;
343 <        register int    i;
343 >        int     i;
344                                  /* open files */
345 <        if ((pfp = fopen(pcf, "r")) == NULL) {
345 >        if ((pfp = fopen(pcf, "rb")) == NULL) {
346                  sprintf(errmsg, "cannot open picture file \"%s\"", pcf);
347                  error(SYSTEM, pcf);
348          }
# Line 298 | Line 350 | char   *pcf, *zbf;
350                  sprintf(errmsg, "cannot open depth file \"%s\"", zbf);
351                  error(SYSTEM, pcf);
352          }
353 +        SET_FD_BINARY(zfd);
354                                  /* load picture header */
355 <        copystruct(&phd.vw, &stdview);
355 >        phd.vw = stdview;
356          phd.expos = 1.0;
357          phd.badfmt = phd.gotview = phd.altprims = 0;
358          if (getheader(pfp, picheadline, &phd) < 0 ||
# Line 313 | Line 366 | char   *pcf, *zbf;
366                  error(USER, errmsg);
367          }
368          if (phd.altprims) {
369 <                sprintf(errmsg, "ignoring primary values in picture \"%s\"",
369 >                sprintf(errmsg, "ignoring color primaries in picture \"%s\"",
370                                  pcf);
371                  error(WARNING, errmsg);
372          }
373                                  /* figure out what to do about exposure */
374 <        if (phd.expos < 0.99 | phd.expos > 1.01) {
374 >        if ((phd.expos < 0.99) | (phd.expos > 1.01)) {
375                  emult = -log(phd.expos)/log(2.);
376                  eshft = emult >= 0. ? emult+.5 : emult-.5;
377                  emult -= (double)eshft;
378 <                if (emult <= 0.01 & emult >= -0.01)
378 >                if ((emult <= 0.01) & (emult >= -0.01))
379                          emult = -1.;
380                  else {
381                          emult = 1./phd.expos;
# Line 335 | Line 388 | char   *pcf, *zbf;
388                                  /* allocate buffers */
389          cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR));
390          zscn = (float *)malloc(scanlen(&prs)*sizeof(float));
391 <        if (cscn == NULL | zscn == NULL)
391 >        if ((cscn == NULL) | (zscn == NULL))
392                  error(SYSTEM, "out of memory in addpicz");
393                                  /* read and process each scanline */
394          for (j = 0; j < numscans(&prs); j++) {
# Line 346 | Line 399 | char   *pcf, *zbf;
399                  }
400                  if (eshft)                              /* shift exposure */
401                          shiftcolrs(cscn, i, eshft);
402 <                i *= sizeof(float);                     /* read depth */
403 <                if (read(zfd, (char *)zscn, i) != i) {
402 >                                                        /* read depth */
403 >                if (read(zfd, zscn, i*sizeof(float)) != i*sizeof(float)) {
404                          sprintf(errmsg, "error reading depth file \"%s\"", zbf);
405                          error(USER, errmsg);
406                  }
407 <                for (i = scanlen(&prs); i--; ) {        /* do each pixel */
407 >                while (i--) {                           /* process each pixel */
408 >                        if (zscn[i] <= 0.0)
409 >                                continue;               /* illegal depth */
410                          pix2loc(vl, &prs, i, j);
411                          aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]);
412                          if (aftd < -FTINY)
# Line 367 | Line 422 | char   *pcf, *zbf;
422                          addray(ro, rd, (double)zscn[i], cscn[i]);
423                  }
424          }
425 +                                /* write output and free beams */
426 +        hdflush(NULL);
427                                  /* clean up */
428 <        free((char *)cscn);
429 <        free((char *)zscn);
428 >        free((void *)cscn);
429 >        free((void *)zscn);
430          fclose(pfp);
431          close(zfd);
432   }
433  
434  
435 < eputs(s)                        /* put error message to stderr */
436 < register char  *s;
435 > void
436 > eputs(                  /* put error message to stderr */
437 >        char  *s
438 > )
439   {
440          static int  midline = 0;
441  
# Line 394 | Line 453 | register char  *s;
453   }
454  
455  
456 < quit(code)                      /* exit the program gracefully */
457 < int     code;
456 > void
457 > quit(                   /* exit the program gracefully */
458 >        int     code
459 > )
460   {
461          hdsync(NULL, 1);        /* write out any buffered data */
462          exit(code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines