| 1 | gregl | 3.1 | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Copy data into a holodeck file | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include "holo.h" | 
| 12 |  |  | #include "view.h" | 
| 13 |  |  | #include "resolu.h" | 
| 14 |  |  |  | 
| 15 |  |  | int     frompicz;               /* input from pictures & depth-buffers? */ | 
| 16 |  |  | int     noutsects;              /* number of output sections */ | 
| 17 | gregl | 3.2 | char    obstr, unobstr;         /* flag pointer values */ | 
| 18 | gregl | 3.1 |  | 
| 19 |  |  | char    *progname;              /* global argv[0] */ | 
| 20 |  |  |  | 
| 21 |  |  |  | 
| 22 |  |  | main(argc, argv) | 
| 23 |  |  | int     argc; | 
| 24 |  |  | char    *argv[]; | 
| 25 |  |  | { | 
| 26 |  |  | int     i; | 
| 27 |  |  |  | 
| 28 |  |  | progname = argv[0]; | 
| 29 |  |  | if (argc < 4) | 
| 30 |  |  | goto userr; | 
| 31 |  |  | if (!strcmp(argv[2], "-h")) | 
| 32 |  |  | frompicz = 0; | 
| 33 |  |  | else if (!strcmp(argv[2], "-pz")) | 
| 34 |  |  | frompicz = 1; | 
| 35 |  |  | else | 
| 36 |  |  | goto userr; | 
| 37 |  |  | if (frompicz && (argc-3)%2) | 
| 38 |  |  | goto userr; | 
| 39 |  |  | noutsects = openholo(argv[1], 1); | 
| 40 |  |  | if (frompicz) | 
| 41 |  |  | for (i = 3; i < argc; i += 2) | 
| 42 |  |  | addpicz(argv[i], argv[i+1]); | 
| 43 |  |  | else | 
| 44 |  |  | for (i = 3; i < argc; i++) | 
| 45 |  |  | addholo(argv[i]); | 
| 46 |  |  | quit(0); | 
| 47 |  |  | userr: | 
| 48 |  |  | fprintf(stderr, "Usage: %s output.hdk -h inp1.hdk ..\n", progname); | 
| 49 |  |  | fprintf(stderr, "   Or: %s output.hdk -pz inp1.pic inp1.zbf ..\n", | 
| 50 |  |  | progname); | 
| 51 |  |  | exit(1); | 
| 52 |  |  | } | 
| 53 |  |  |  | 
| 54 |  |  |  | 
| 55 | gregl | 3.2 | #define H_BADF  01 | 
| 56 |  |  | #define H_OBST  02 | 
| 57 |  |  | #define H_OBSF  04 | 
| 58 |  |  |  | 
| 59 |  |  | holheadline(s, hf)              /* check holodeck header line */ | 
| 60 | gregl | 3.1 | register char   *s; | 
| 61 | gregl | 3.2 | int     *hf; | 
| 62 | gregl | 3.1 | { | 
| 63 |  |  | char    fmt[32]; | 
| 64 |  |  |  | 
| 65 |  |  | if (formatval(fmt, s)) { | 
| 66 | gregl | 3.2 | if (strcmp(fmt, HOLOFMT)) | 
| 67 |  |  | *hf |= H_BADF; | 
| 68 |  |  | else | 
| 69 |  |  | *hf &= ~H_BADF; | 
| 70 | gregl | 3.1 | return; | 
| 71 |  |  | } | 
| 72 |  |  | if (!strncmp(s, "OBSTRUCTIONS=", 13)) { | 
| 73 |  |  | s += 13; | 
| 74 |  |  | while (*s == ' ') s++; | 
| 75 |  |  | if (*s == 't' | *s == 'T') | 
| 76 | gregl | 3.2 | *hf |= H_OBST; | 
| 77 | gregl | 3.1 | else if (*s == 'f' | *s == 'F') | 
| 78 | gregl | 3.2 | *hf |= H_OBSF; | 
| 79 | gregl | 3.1 | else | 
| 80 |  |  | error(WARNING, "bad OBSTRUCTIONS value in holodeck"); | 
| 81 |  |  | return; | 
| 82 |  |  | } | 
| 83 |  |  | } | 
| 84 |  |  |  | 
| 85 |  |  | int | 
| 86 |  |  | openholo(fname, append)         /* open existing holodeck file for i/o */ | 
| 87 |  |  | char    *fname; | 
| 88 |  |  | int     append; | 
| 89 |  |  | { | 
| 90 |  |  | extern long     ftell(); | 
| 91 |  |  | FILE    *fp; | 
| 92 |  |  | int     fd; | 
| 93 | gregl | 3.2 | int     hflags = 0; | 
| 94 | gregl | 3.3 | long    nextloc; | 
| 95 | gregl | 3.1 | int     n; | 
| 96 |  |  | /* open holodeck file */ | 
| 97 |  |  | if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) { | 
| 98 |  |  | sprintf(errmsg, "cannot open \"%s\" for %s", fname, | 
| 99 |  |  | append ? "appending" : "reading"); | 
| 100 |  |  | error(SYSTEM, errmsg); | 
| 101 |  |  | } | 
| 102 |  |  | /* check header and magic number */ | 
| 103 | gregl | 3.2 | if (getheader(fp, holheadline, &hflags) < 0 || | 
| 104 |  |  | hflags&H_BADF || getw(fp) != HOLOMAGIC) { | 
| 105 | gregl | 3.1 | sprintf(errmsg, "file \"%s\" not in holodeck format", fname); | 
| 106 |  |  | error(USER, errmsg); | 
| 107 |  |  | } | 
| 108 |  |  | fd = dup(fileno(fp));                   /* dup file handle */ | 
| 109 |  |  | nextloc = ftell(fp);                    /* get stdio position */ | 
| 110 |  |  | fclose(fp);                             /* done with stdio */ | 
| 111 |  |  | for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 112 | gregl | 3.3 | lseek(fd, nextloc, 0); | 
| 113 | gregl | 3.1 | read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 114 | gregl | 3.2 | hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr : | 
| 115 |  |  | hflags&H_OBSF ? &unobstr : (char *)NULL; | 
| 116 | gregl | 3.1 | } | 
| 117 |  |  | return(n); | 
| 118 |  |  | } | 
| 119 |  |  |  | 
| 120 | gregl | 3.2 | #undef H_BADF | 
| 121 |  |  | #undef H_OBST | 
| 122 |  |  | #undef H_OBSF | 
| 123 | gregl | 3.1 |  | 
| 124 | gregl | 3.2 |  | 
| 125 | gregl | 3.1 | addray(ro, rd, d, cv)           /* add a ray to our output holodeck */ | 
| 126 |  |  | FVECT   ro, rd; | 
| 127 |  |  | double  d; | 
| 128 |  |  | COLR    cv; | 
| 129 |  |  | { | 
| 130 |  |  | int     sn; | 
| 131 |  |  | register HOLO   *hp; | 
| 132 |  |  | GCOORD  gc[2]; | 
| 133 |  |  | BYTE    rr[2][2]; | 
| 134 |  |  | double  d0, d1; | 
| 135 |  |  | register RAYVAL *rv; | 
| 136 |  |  | /* check each output section */ | 
| 137 |  |  | for (sn = noutsects; sn--; ) { | 
| 138 |  |  | hp = hdlist[sn]; | 
| 139 |  |  | d0 = hdinter(gc, rr, &d1, hp, ro, rd); | 
| 140 |  |  | if (d <= d0 || d1 < -0.001) | 
| 141 |  |  | continue;       /* missed section */ | 
| 142 | gregl | 3.2 | if (hp->priv == &obstr && d0 < -0.001) | 
| 143 | gregl | 3.1 | continue;       /* ray starts too late */ | 
| 144 | gregl | 3.2 | if (hp->priv == &unobstr && d < 0.999*d1) | 
| 145 | gregl | 3.1 | continue;       /* ray ends too soon */ | 
| 146 |  |  | /* should we check for duplicates? */ | 
| 147 |  |  | rv = hdnewrays(hp, hdbindex(hp, gc), 1); | 
| 148 |  |  | rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1]; | 
| 149 |  |  | rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1]; | 
| 150 |  |  | copycolr(rv->v, cv); | 
| 151 |  |  | rv->d = hdcode(hp, d-d0); | 
| 152 |  |  | } | 
| 153 |  |  | } | 
| 154 |  |  |  | 
| 155 |  |  |  | 
| 156 |  |  | addholo(hdf)                    /* add a holodeck file */ | 
| 157 |  |  | char    *hdf; | 
| 158 |  |  | { | 
| 159 |  |  | int     fd; | 
| 160 |  |  | register HOLO   *hp; | 
| 161 |  |  | register BEAM   *bp; | 
| 162 |  |  | GCOORD  gc[2]; | 
| 163 |  |  | FVECT   ro, rd; | 
| 164 |  |  | double  d; | 
| 165 |  |  | int     i, j; | 
| 166 |  |  | register int    k; | 
| 167 |  |  |  | 
| 168 |  |  | openholo(hdf, 0);               /* open the holodeck for reading */ | 
| 169 |  |  | fd = hdlist[noutsects]->fd;     /* remember the file handle */ | 
| 170 |  |  | while ((hp = hdlist[noutsects]) != NULL) {      /* load each section */ | 
| 171 |  |  | for (j = nbeams(hp); j > 0; j--)        /* load each beam */ | 
| 172 |  |  | if ((bp = hdgetbeam(hp, j)) != NULL) { | 
| 173 |  |  | hdbcoord(gc, hp, j); | 
| 174 |  |  | for (k = bp->nrm; k--; ) { | 
| 175 | gregl | 3.2 | d = hdray(ro, rd, | 
| 176 |  |  | hp, gc, hdbray(bp)[k].r); | 
| 177 |  |  | if (hp->priv == &unobstr) | 
| 178 |  |  | VSUM(ro, ro, rd, d); | 
| 179 |  |  | else | 
| 180 |  |  | d = 0.; | 
| 181 |  |  | d = hddepth(hp, hdbray(bp)[k].d) - d; | 
| 182 | gregl | 3.1 | addray(ro, rd, d, hdbray(bp)[k].v); | 
| 183 |  |  | } | 
| 184 |  |  | hdfreebeam(hp, j);      /* free the beam */ | 
| 185 |  |  | } | 
| 186 |  |  | hddone(hp);                             /* free the section */ | 
| 187 |  |  | } | 
| 188 |  |  | close(fd);                      /* close the file */ | 
| 189 |  |  | } | 
| 190 |  |  |  | 
| 191 |  |  |  | 
| 192 |  |  | struct phead { | 
| 193 |  |  | VIEW    vw; | 
| 194 |  |  | double  expos; | 
| 195 |  |  | short   gotview; | 
| 196 |  |  | short   badfmt; | 
| 197 |  |  | short   altprims; | 
| 198 |  |  | }; | 
| 199 |  |  |  | 
| 200 |  |  |  | 
| 201 |  |  | picheadline(s, ph)              /* process picture header line */ | 
| 202 |  |  | char    *s; | 
| 203 |  |  | struct phead    *ph; | 
| 204 |  |  | { | 
| 205 |  |  | char    fmt[32]; | 
| 206 |  |  |  | 
| 207 |  |  | if (formatval(fmt, s)) { | 
| 208 |  |  | ph->badfmt = strcmp(fmt, COLRFMT); | 
| 209 |  |  | return; | 
| 210 |  |  | } | 
| 211 |  |  | if (isprims(s)) { | 
| 212 |  |  | ph->altprims++;         /* don't want to deal with this */ | 
| 213 |  |  | return; | 
| 214 |  |  | } | 
| 215 |  |  | if (isexpos(s)) { | 
| 216 |  |  | ph->expos *= exposval(s); | 
| 217 |  |  | return; | 
| 218 |  |  | } | 
| 219 |  |  | if (isview(s)) { | 
| 220 |  |  | ph->gotview += sscanview(&ph->vw, s); | 
| 221 |  |  | return; | 
| 222 |  |  | } | 
| 223 |  |  | } | 
| 224 |  |  |  | 
| 225 |  |  |  | 
| 226 |  |  | addpicz(pcf, zbf)               /* add a picture + depth-buffer */ | 
| 227 |  |  | char    *pcf, *zbf; | 
| 228 |  |  | { | 
| 229 |  |  | FILE    *pfp; | 
| 230 |  |  | int     zfd; | 
| 231 |  |  | COLR    *cscn; | 
| 232 |  |  | float   *zscn; | 
| 233 |  |  | struct phead    phd; | 
| 234 |  |  | int     eshft; | 
| 235 |  |  | double  emult; | 
| 236 |  |  | RESOLU  prs; | 
| 237 |  |  | FLOAT   vl[2]; | 
| 238 |  |  | FVECT   ro, rd; | 
| 239 |  |  | double  aftd; | 
| 240 |  |  | COLOR   ctmp; | 
| 241 |  |  | int     j; | 
| 242 |  |  | register int    i; | 
| 243 |  |  | /* open files */ | 
| 244 |  |  | if ((pfp = fopen(pcf, "r")) == NULL) { | 
| 245 |  |  | sprintf(errmsg, "cannot open picture file \"%s\"", pcf); | 
| 246 |  |  | error(SYSTEM, pcf); | 
| 247 |  |  | } | 
| 248 |  |  | if ((zfd = open(zbf, O_RDONLY)) < 0) { | 
| 249 |  |  | sprintf(errmsg, "cannot open depth file \"%s\"", zbf); | 
| 250 |  |  | error(SYSTEM, pcf); | 
| 251 |  |  | } | 
| 252 |  |  | /* load picture header */ | 
| 253 |  |  | copystruct(&phd.vw, &stdview); | 
| 254 |  |  | phd.expos = 1.0; | 
| 255 |  |  | phd.badfmt = phd.gotview = phd.altprims = 0; | 
| 256 |  |  | if (getheader(pfp, picheadline, &phd) < 0 || | 
| 257 |  |  | phd.badfmt || !fgetsresolu(&prs, pfp)) { | 
| 258 |  |  | sprintf(errmsg, "bad format for picture file \"%s\"", pcf); | 
| 259 |  |  | error(USER, errmsg); | 
| 260 |  |  | } | 
| 261 |  |  | if (!phd.gotview || setview(&phd.vw) != NULL) { | 
| 262 |  |  | sprintf(errmsg, "missing/illegal view in picture \"%s\"", | 
| 263 |  |  | pcf); | 
| 264 |  |  | error(USER, errmsg); | 
| 265 |  |  | } | 
| 266 |  |  | if (phd.altprims) { | 
| 267 |  |  | sprintf(errmsg, "ignoring primary values in picture \"%s\"", | 
| 268 |  |  | pcf); | 
| 269 |  |  | error(WARNING, errmsg); | 
| 270 |  |  | } | 
| 271 |  |  | /* figure out what to do about exposure */ | 
| 272 |  |  | if (phd.expos < 0.99 | phd.expos > 1.01) { | 
| 273 |  |  | emult = -log(phd.expos)/log(2.); | 
| 274 |  |  | eshft = emult >= 0. ? emult+.5 : emult-.5; | 
| 275 |  |  | emult -= (double)eshft; | 
| 276 |  |  | if (emult <= 0.01 & emult >= -0.01) | 
| 277 |  |  | emult = -1.; | 
| 278 |  |  | else { | 
| 279 |  |  | emult = 1./phd.expos; | 
| 280 |  |  | eshft = 0; | 
| 281 |  |  | } | 
| 282 |  |  | } else { | 
| 283 |  |  | emult = -1.; | 
| 284 |  |  | eshft = 0; | 
| 285 |  |  | } | 
| 286 |  |  | /* allocate buffers */ | 
| 287 |  |  | cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR)); | 
| 288 |  |  | zscn = (float *)malloc(scanlen(&prs)*sizeof(float)); | 
| 289 |  |  | if (cscn == NULL | zscn == NULL) | 
| 290 |  |  | error(SYSTEM, "out of memory in addpicz"); | 
| 291 |  |  | /* read and process each scanline */ | 
| 292 |  |  | for (j = 0; j < numscans(&prs); j++) { | 
| 293 |  |  | i = scanlen(&prs);                      /* read colrs */ | 
| 294 |  |  | if (freadcolrs(cscn, i, pfp) < 0) { | 
| 295 |  |  | sprintf(errmsg, "error reading picture \"%s\"", pcf); | 
| 296 |  |  | error(USER, errmsg); | 
| 297 |  |  | } | 
| 298 |  |  | if (eshft)                              /* shift exposure */ | 
| 299 |  |  | shiftcolrs(cscn, i, eshft); | 
| 300 |  |  | i *= sizeof(float);                     /* read depth */ | 
| 301 |  |  | if (read(zfd, (char *)zscn, i) != i) { | 
| 302 |  |  | sprintf(errmsg, "error reading depth file \"%s\"", zbf); | 
| 303 |  |  | error(USER, errmsg); | 
| 304 |  |  | } | 
| 305 |  |  | for (i = scanlen(&prs); i--; ) {        /* do each pixel */ | 
| 306 |  |  | pix2loc(vl, &prs, i, j); | 
| 307 |  |  | aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]); | 
| 308 |  |  | if (aftd < -FTINY) | 
| 309 |  |  | continue;               /* off view */ | 
| 310 |  |  | if (aftd > FTINY && zscn[i] > aftd) | 
| 311 |  |  | continue;               /* aft clipped */ | 
| 312 |  |  | if (emult > 0.) {               /* whatta pain */ | 
| 313 |  |  | colr_color(ctmp, cscn[i]); | 
| 314 |  |  | scalecolor(ctmp, emult); | 
| 315 |  |  | setcolr(cscn[i], colval(ctmp,RED), | 
| 316 |  |  | colval(ctmp,GRN), colval(ctmp,BLU)); | 
| 317 |  |  | } | 
| 318 |  |  | addray(ro, rd, (double)zscn[i], cscn[i]); | 
| 319 |  |  | } | 
| 320 |  |  | } | 
| 321 |  |  | /* clean up */ | 
| 322 |  |  | free((char *)cscn); | 
| 323 |  |  | free((char *)zscn); | 
| 324 |  |  | fclose(pfp); | 
| 325 |  |  | close(zfd); | 
| 326 |  |  | } | 
| 327 |  |  |  | 
| 328 |  |  |  | 
| 329 |  |  | eputs(s)                        /* put error message to stderr */ | 
| 330 |  |  | register char  *s; | 
| 331 |  |  | { | 
| 332 |  |  | static int  midline = 0; | 
| 333 |  |  |  | 
| 334 |  |  | if (!*s) | 
| 335 |  |  | return; | 
| 336 |  |  | if (!midline++) {       /* prepend line with program name */ | 
| 337 |  |  | fputs(progname, stderr); | 
| 338 |  |  | fputs(": ", stderr); | 
| 339 |  |  | } | 
| 340 |  |  | fputs(s, stderr); | 
| 341 |  |  | if (s[strlen(s)-1] == '\n') { | 
| 342 |  |  | fflush(stderr); | 
| 343 |  |  | midline = 0; | 
| 344 |  |  | } | 
| 345 |  |  | } | 
| 346 |  |  |  | 
| 347 |  |  |  | 
| 348 |  |  | quit(code)                      /* exit the program gracefully */ | 
| 349 |  |  | int     code; | 
| 350 |  |  | { | 
| 351 |  |  | hdsync(NULL, 1);        /* write out any buffered data */ | 
| 352 |  |  | exit(code); | 
| 353 |  |  | } |