| 1 | 
gregl | 
3.1 | 
#ifndef lint | 
| 2 | 
greg | 
3.30 | 
static const char       RCSid[] = "$Id: rhcopy.c,v 3.29 2017/05/03 21:15:06 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 | 
  | 
  | 
 | 
| 17 | 
gwlarson | 
3.14 | 
int     checkdepth = 1;         /* check depth (!-d option)? */ | 
| 18 | 
  | 
  | 
int     checkrepeats = 0;       /* check for repeats (-u option)? */ | 
| 19 | 
gregl | 
3.1 | 
int     frompicz;               /* input from pictures & depth-buffers? */ | 
| 20 | 
  | 
  | 
int     noutsects;              /* number of output sections */ | 
| 21 | 
gregl | 
3.2 | 
char    obstr, unobstr;         /* flag pointer values */ | 
| 22 | 
gregl | 
3.1 | 
 | 
| 23 | 
  | 
  | 
char    *progname;              /* global argv[0] */ | 
| 24 | 
  | 
  | 
 | 
| 25 | 
schorsch | 
3.22 | 
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 | 
schorsch | 
3.23 | 
static gethfunc holheadline; | 
| 35 | 
schorsch | 
3.22 | 
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 | 
schorsch | 
3.23 | 
static gethfunc picheadline; | 
| 39 | 
schorsch | 
3.22 | 
static void addpicz(char *pcf, char *zbf); | 
| 40 | 
  | 
  | 
 | 
| 41 | 
  | 
  | 
 | 
| 42 | 
gregl | 
3.1 | 
 | 
| 43 | 
schorsch | 
3.22 | 
int | 
| 44 | 
  | 
  | 
main( | 
| 45 | 
  | 
  | 
        int     argc, | 
| 46 | 
  | 
  | 
        char    *argv[] | 
| 47 | 
  | 
  | 
) | 
| 48 | 
gregl | 
3.1 | 
{ | 
| 49 | 
  | 
  | 
        int     i; | 
| 50 | 
  | 
  | 
 | 
| 51 | 
  | 
  | 
        progname = argv[0]; | 
| 52 | 
gregl | 
3.5 | 
        frompicz = -1; | 
| 53 | 
  | 
  | 
        for (i = 2; i < argc && argv[i][0] == '-'; i++) | 
| 54 | 
  | 
  | 
                switch (argv[i][1]) { | 
| 55 | 
gregl | 
3.8 | 
                case 'u': | 
| 56 | 
gregl | 
3.5 | 
                        checkrepeats = 1; | 
| 57 | 
  | 
  | 
                        break; | 
| 58 | 
gregl | 
3.8 | 
                case 'd': | 
| 59 | 
gregl | 
3.5 | 
                        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 | 
gregl | 
3.1 | 
                goto userr; | 
| 72 | 
gregl | 
3.5 | 
        if (frompicz && (argc-i)%2) | 
| 73 | 
gregl | 
3.1 | 
                goto userr; | 
| 74 | 
  | 
  | 
        noutsects = openholo(argv[1], 1); | 
| 75 | 
gwlarson | 
3.10 | 
        if (frompicz) { | 
| 76 | 
gregl | 
3.5 | 
                for ( ; i < argc; i += 2) | 
| 77 | 
gregl | 
3.1 | 
                        addpicz(argv[i], argv[i+1]); | 
| 78 | 
gwlarson | 
3.10 | 
        } else { | 
| 79 | 
  | 
  | 
                if (BKBSIZE*1024*1.5 > hdcachesize) | 
| 80 | 
  | 
  | 
                        hdcachesize = BKBSIZE*1024*1.5; | 
| 81 | 
gregl | 
3.5 | 
                for ( ; i < argc; i++) | 
| 82 | 
gregl | 
3.1 | 
                        addholo(argv[i]); | 
| 83 | 
gwlarson | 
3.10 | 
        } | 
| 84 | 
gregl | 
3.1 | 
        quit(0); | 
| 85 | 
  | 
  | 
userr: | 
| 86 | 
gregl | 
3.8 | 
        fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n", | 
| 87 | 
gregl | 
3.1 | 
                        progname); | 
| 88 | 
greg | 
3.24 | 
        fprintf(stderr, "   Or: %s output.hdk [-u][-d] -p inp1.hdr inp1.zbf ..\n", | 
| 89 | 
gregl | 
3.5 | 
                        progname); | 
| 90 | 
gregl | 
3.1 | 
        exit(1); | 
| 91 | 
  | 
  | 
} | 
| 92 | 
  | 
  | 
 | 
| 93 | 
  | 
  | 
 | 
| 94 | 
gregl | 
3.2 | 
#define H_BADF  01 | 
| 95 | 
  | 
  | 
#define H_OBST  02 | 
| 96 | 
  | 
  | 
#define H_OBSF  04 | 
| 97 | 
  | 
  | 
 | 
| 98 | 
schorsch | 
3.23 | 
static int | 
| 99 | 
schorsch | 
3.22 | 
holheadline(            /* check holodeck header line */ | 
| 100 | 
  | 
  | 
        register char   *s, | 
| 101 | 
schorsch | 
3.23 | 
        void    *vhf | 
| 102 | 
schorsch | 
3.22 | 
) | 
| 103 | 
gregl | 
3.1 | 
{ | 
| 104 | 
greg | 
3.30 | 
        char    fmt[MAXFMTLEN]; | 
| 105 | 
schorsch | 
3.23 | 
        int     *hf = vhf; | 
| 106 | 
gregl | 
3.1 | 
 | 
| 107 | 
  | 
  | 
        if (formatval(fmt, s)) { | 
| 108 | 
gregl | 
3.2 | 
                if (strcmp(fmt, HOLOFMT)) | 
| 109 | 
  | 
  | 
                        *hf |= H_BADF; | 
| 110 | 
  | 
  | 
                else | 
| 111 | 
  | 
  | 
                        *hf &= ~H_BADF; | 
| 112 | 
gwlarson | 
3.9 | 
                return(0); | 
| 113 | 
gregl | 
3.1 | 
        } | 
| 114 | 
  | 
  | 
        if (!strncmp(s, "OBSTRUCTIONS=", 13)) { | 
| 115 | 
  | 
  | 
                s += 13; | 
| 116 | 
  | 
  | 
                while (*s == ' ') s++; | 
| 117 | 
schorsch | 
3.19 | 
                if ((*s == 't') | (*s == 'T')) | 
| 118 | 
gregl | 
3.2 | 
                        *hf |= H_OBST; | 
| 119 | 
schorsch | 
3.19 | 
                else if ((*s == 'f') | (*s == 'F')) | 
| 120 | 
gregl | 
3.2 | 
                        *hf |= H_OBSF; | 
| 121 | 
gregl | 
3.1 | 
                else | 
| 122 | 
  | 
  | 
                        error(WARNING, "bad OBSTRUCTIONS value in holodeck"); | 
| 123 | 
gwlarson | 
3.9 | 
                return(0); | 
| 124 | 
gregl | 
3.1 | 
        } | 
| 125 | 
gwlarson | 
3.9 | 
        return(0); | 
| 126 | 
gregl | 
3.1 | 
} | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
int | 
| 129 | 
schorsch | 
3.22 | 
openholo(               /* open existing holodeck file for i/o */ | 
| 130 | 
  | 
  | 
        char    *fname, | 
| 131 | 
  | 
  | 
        int     append | 
| 132 | 
  | 
  | 
) | 
| 133 | 
gregl | 
3.1 | 
{ | 
| 134 | 
  | 
  | 
        FILE    *fp; | 
| 135 | 
  | 
  | 
        int     fd; | 
| 136 | 
gregl | 
3.2 | 
        int     hflags = 0; | 
| 137 | 
greg | 
3.26 | 
        off_t   nextloc; | 
| 138 | 
gregl | 
3.1 | 
        int     n; | 
| 139 | 
  | 
  | 
                                        /* open holodeck file */ | 
| 140 | 
  | 
  | 
        if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) { | 
| 141 | 
  | 
  | 
                sprintf(errmsg, "cannot open \"%s\" for %s", fname, | 
| 142 | 
  | 
  | 
                                append ? "appending" : "reading"); | 
| 143 | 
  | 
  | 
                error(SYSTEM, errmsg); | 
| 144 | 
  | 
  | 
        } | 
| 145 | 
  | 
  | 
                                        /* check header and magic number */ | 
| 146 | 
schorsch | 
3.23 | 
        if (getheader(fp, holheadline, &hflags) < 0 || | 
| 147 | 
gregl | 
3.2 | 
                        hflags&H_BADF || getw(fp) != HOLOMAGIC) { | 
| 148 | 
gregl | 
3.1 | 
                sprintf(errmsg, "file \"%s\" not in holodeck format", fname); | 
| 149 | 
  | 
  | 
                error(USER, errmsg); | 
| 150 | 
  | 
  | 
        } | 
| 151 | 
  | 
  | 
        fd = dup(fileno(fp));                   /* dup file handle */ | 
| 152 | 
  | 
  | 
        nextloc = ftell(fp);                    /* get stdio position */ | 
| 153 | 
  | 
  | 
        fclose(fp);                             /* done with stdio */ | 
| 154 | 
  | 
  | 
        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 155 | 
greg | 
3.26 | 
                lseek(fd, nextloc, SEEK_SET); | 
| 156 | 
gregl | 
3.1 | 
                read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 157 | 
gregl | 
3.2 | 
                hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr : | 
| 158 | 
  | 
  | 
                                hflags&H_OBSF ? &unobstr : (char *)NULL; | 
| 159 | 
gregl | 
3.1 | 
        } | 
| 160 | 
  | 
  | 
        return(n); | 
| 161 | 
  | 
  | 
} | 
| 162 | 
  | 
  | 
 | 
| 163 | 
gregl | 
3.2 | 
#undef H_BADF | 
| 164 | 
  | 
  | 
#undef H_OBST | 
| 165 | 
  | 
  | 
#undef H_OBSF | 
| 166 | 
gregl | 
3.1 | 
 | 
| 167 | 
gregl | 
3.2 | 
 | 
| 168 | 
schorsch | 
3.22 | 
void | 
| 169 | 
  | 
  | 
addray(         /* add a ray to our output holodeck */ | 
| 170 | 
  | 
  | 
        FVECT   ro, | 
| 171 | 
  | 
  | 
        FVECT   rd, | 
| 172 | 
  | 
  | 
        double  d, | 
| 173 | 
  | 
  | 
        COLR    cv | 
| 174 | 
  | 
  | 
) | 
| 175 | 
gregl | 
3.1 | 
{ | 
| 176 | 
gregl | 
3.5 | 
        int     sn, bi, n; | 
| 177 | 
gregl | 
3.1 | 
        register HOLO   *hp; | 
| 178 | 
  | 
  | 
        GCOORD  gc[2]; | 
| 179 | 
greg | 
3.27 | 
        uby8    rr[2][2]; | 
| 180 | 
gregl | 
3.5 | 
        BEAM    *bp; | 
| 181 | 
gregl | 
3.1 | 
        double  d0, d1; | 
| 182 | 
gregl | 
3.5 | 
        unsigned        dc; | 
| 183 | 
gregl | 
3.1 | 
        register RAYVAL *rv; | 
| 184 | 
  | 
  | 
                                /* check each output section */ | 
| 185 | 
  | 
  | 
        for (sn = noutsects; sn--; ) { | 
| 186 | 
  | 
  | 
                hp = hdlist[sn]; | 
| 187 | 
  | 
  | 
                d0 = hdinter(gc, rr, &d1, hp, ro, rd); | 
| 188 | 
  | 
  | 
                if (d <= d0 || d1 < -0.001) | 
| 189 | 
  | 
  | 
                        continue;       /* missed section */ | 
| 190 | 
gregl | 
3.5 | 
                if (checkdepth) {               /* check depth */ | 
| 191 | 
  | 
  | 
                        if (hp->priv == &obstr && d0 < -0.001) | 
| 192 | 
  | 
  | 
                                continue;       /* ray starts too late */ | 
| 193 | 
  | 
  | 
                        if (hp->priv == &unobstr && d < 0.999*d1) | 
| 194 | 
  | 
  | 
                                continue;       /* ray ends too soon */ | 
| 195 | 
  | 
  | 
                } | 
| 196 | 
  | 
  | 
                dc = hdcode(hp, d-d0); | 
| 197 | 
  | 
  | 
                bi = hdbindex(hp, gc);          /* check for duplicates */ | 
| 198 | 
  | 
  | 
                if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) { | 
| 199 | 
  | 
  | 
                        for (n = bp->nrm, rv = hdbray(bp); n--; rv++) | 
| 200 | 
gwlarson | 
3.12 | 
                                if ((hp->priv != NULL || rv->d == dc) && | 
| 201 | 
gregl | 
3.5 | 
                                                rv->r[0][0] == rr[0][0] && | 
| 202 | 
  | 
  | 
                                                rv->r[0][1] == rr[0][1] && | 
| 203 | 
  | 
  | 
                                                rv->r[1][0] == rr[1][0] && | 
| 204 | 
  | 
  | 
                                                rv->r[1][1] == rr[1][1]) | 
| 205 | 
  | 
  | 
                                        break; | 
| 206 | 
  | 
  | 
                        if (n >= 0) | 
| 207 | 
  | 
  | 
                                continue;       /* found a matching ray */ | 
| 208 | 
  | 
  | 
                } | 
| 209 | 
  | 
  | 
                rv = hdnewrays(hp, bi, 1); | 
| 210 | 
  | 
  | 
                rv->d = dc; | 
| 211 | 
gregl | 
3.1 | 
                rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1]; | 
| 212 | 
  | 
  | 
                rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1]; | 
| 213 | 
  | 
  | 
                copycolr(rv->v, cv); | 
| 214 | 
  | 
  | 
        } | 
| 215 | 
  | 
  | 
} | 
| 216 | 
  | 
  | 
 | 
| 217 | 
  | 
  | 
 | 
| 218 | 
gwlarson | 
3.11 | 
static BEAMI    *beamdir; | 
| 219 | 
  | 
  | 
 | 
| 220 | 
  | 
  | 
static int | 
| 221 | 
schorsch | 
3.22 | 
bpcmp(                  /* compare beam positions on disk */ | 
| 222 | 
  | 
  | 
        const void      *b1p, | 
| 223 | 
  | 
  | 
        const void      *b2p | 
| 224 | 
  | 
  | 
) | 
| 225 | 
gregl | 
3.1 | 
{ | 
| 226 | 
schorsch | 
3.22 | 
        register off_t  pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo; | 
| 227 | 
gwlarson | 
3.11 | 
 | 
| 228 | 
  | 
  | 
        if (pdif > 0L) return(1); | 
| 229 | 
  | 
  | 
        if (pdif < 0L) return(-1); | 
| 230 | 
  | 
  | 
        return(0); | 
| 231 | 
  | 
  | 
} | 
| 232 | 
  | 
  | 
 | 
| 233 | 
  | 
  | 
static int | 
| 234 | 
schorsch | 
3.22 | 
addclump(               /* transfer the given clump and free */ | 
| 235 | 
  | 
  | 
        HOLO    *hp, | 
| 236 | 
  | 
  | 
        int     *bq, | 
| 237 | 
  | 
  | 
        int     nb | 
| 238 | 
  | 
  | 
) | 
| 239 | 
gwlarson | 
3.11 | 
{ | 
| 240 | 
gregl | 
3.1 | 
        GCOORD  gc[2]; | 
| 241 | 
  | 
  | 
        FVECT   ro, rd; | 
| 242 | 
  | 
  | 
        double  d; | 
| 243 | 
gwlarson | 
3.11 | 
        int     i; | 
| 244 | 
gregl | 
3.1 | 
        register int    k; | 
| 245 | 
gwlarson | 
3.11 | 
        register BEAM   *bp; | 
| 246 | 
  | 
  | 
                                        /* sort based on file position */ | 
| 247 | 
  | 
  | 
        beamdir = hp->bi; | 
| 248 | 
  | 
  | 
        qsort((char *)bq, nb, sizeof(*bq), bpcmp); | 
| 249 | 
  | 
  | 
                                        /* transfer each beam */ | 
| 250 | 
  | 
  | 
        for (i = 0; i < nb; i++) { | 
| 251 | 
  | 
  | 
                bp = hdgetbeam(hp, bq[i]); | 
| 252 | 
  | 
  | 
                hdbcoord(gc, hp, bq[i]); | 
| 253 | 
  | 
  | 
                                                /* add each ray to output */ | 
| 254 | 
  | 
  | 
                for (k = bp->nrm; k--; ) { | 
| 255 | 
  | 
  | 
                        d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r); | 
| 256 | 
  | 
  | 
                        if (hp->priv == &unobstr) | 
| 257 | 
  | 
  | 
                                VSUM(ro, ro, rd, d); | 
| 258 | 
  | 
  | 
                        else | 
| 259 | 
  | 
  | 
                                d = 0.; | 
| 260 | 
  | 
  | 
                        d = hddepth(hp, hdbray(bp)[k].d) - d; | 
| 261 | 
  | 
  | 
                        addray(ro, rd, d, hdbray(bp)[k].v); | 
| 262 | 
  | 
  | 
                } | 
| 263 | 
  | 
  | 
                hdfreebeam(hp, bq[i]);          /* free the beam */ | 
| 264 | 
gwlarson | 
3.10 | 
        } | 
| 265 | 
gwlarson | 
3.11 | 
        return(0); | 
| 266 | 
gwlarson | 
3.10 | 
} | 
| 267 | 
  | 
  | 
 | 
| 268 | 
schorsch | 
3.22 | 
 | 
| 269 | 
  | 
  | 
void | 
| 270 | 
  | 
  | 
addholo(                        /* add a holodeck file */ | 
| 271 | 
  | 
  | 
        char    *hdf | 
| 272 | 
  | 
  | 
) | 
| 273 | 
gwlarson | 
3.10 | 
{ | 
| 274 | 
  | 
  | 
        int     fd; | 
| 275 | 
gregl | 
3.4 | 
                                        /* open the holodeck for reading */ | 
| 276 | 
  | 
  | 
        openholo(hdf, 0); | 
| 277 | 
gregl | 
3.1 | 
        fd = hdlist[noutsects]->fd;     /* remember the file handle */ | 
| 278 | 
gwlarson | 
3.10 | 
        while (hdlist[noutsects] != NULL) {     /* load each section */ | 
| 279 | 
  | 
  | 
                                                        /* clump the beams */ | 
| 280 | 
gwlarson | 
3.11 | 
                clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump); | 
| 281 | 
gwlarson | 
3.10 | 
                hddone(hdlist[noutsects]);              /* free the section */ | 
| 282 | 
gregl | 
3.1 | 
        } | 
| 283 | 
gwlarson | 
3.10 | 
        close(fd);                      /* close input file */ | 
| 284 | 
gwlarson | 
3.13 | 
        hdflush(NULL);                  /* flush output */ | 
| 285 | 
gregl | 
3.1 | 
} | 
| 286 | 
  | 
  | 
 | 
| 287 | 
  | 
  | 
 | 
| 288 | 
  | 
  | 
 | 
| 289 | 
schorsch | 
3.23 | 
static int | 
| 290 | 
schorsch | 
3.22 | 
picheadline(            /* process picture header line */ | 
| 291 | 
  | 
  | 
        char    *s, | 
| 292 | 
schorsch | 
3.23 | 
        void    *vph | 
| 293 | 
schorsch | 
3.22 | 
) | 
| 294 | 
gregl | 
3.1 | 
{ | 
| 295 | 
  | 
  | 
        char    fmt[32]; | 
| 296 | 
schorsch | 
3.23 | 
        struct phead *ph = vph; | 
| 297 | 
gregl | 
3.1 | 
 | 
| 298 | 
  | 
  | 
        if (formatval(fmt, s)) { | 
| 299 | 
  | 
  | 
                ph->badfmt = strcmp(fmt, COLRFMT); | 
| 300 | 
gwlarson | 
3.9 | 
                return(0); | 
| 301 | 
gregl | 
3.1 | 
        } | 
| 302 | 
  | 
  | 
        if (isprims(s)) { | 
| 303 | 
  | 
  | 
                ph->altprims++;         /* don't want to deal with this */ | 
| 304 | 
gwlarson | 
3.9 | 
                return(0); | 
| 305 | 
gregl | 
3.1 | 
        } | 
| 306 | 
  | 
  | 
        if (isexpos(s)) { | 
| 307 | 
  | 
  | 
                ph->expos *= exposval(s); | 
| 308 | 
gwlarson | 
3.9 | 
                return(0); | 
| 309 | 
gregl | 
3.1 | 
        } | 
| 310 | 
  | 
  | 
        if (isview(s)) { | 
| 311 | 
  | 
  | 
                ph->gotview += sscanview(&ph->vw, s); | 
| 312 | 
gwlarson | 
3.9 | 
                return(0); | 
| 313 | 
gregl | 
3.1 | 
        } | 
| 314 | 
gwlarson | 
3.9 | 
        return(0); | 
| 315 | 
gregl | 
3.1 | 
} | 
| 316 | 
  | 
  | 
 | 
| 317 | 
  | 
  | 
 | 
| 318 | 
schorsch | 
3.22 | 
void | 
| 319 | 
  | 
  | 
addpicz(                /* add a picture + depth-buffer */ | 
| 320 | 
  | 
  | 
        char    *pcf, | 
| 321 | 
  | 
  | 
        char    *zbf | 
| 322 | 
  | 
  | 
) | 
| 323 | 
gregl | 
3.1 | 
{ | 
| 324 | 
  | 
  | 
        FILE    *pfp; | 
| 325 | 
  | 
  | 
        int     zfd; | 
| 326 | 
  | 
  | 
        COLR    *cscn; | 
| 327 | 
  | 
  | 
        float   *zscn; | 
| 328 | 
  | 
  | 
        struct phead    phd; | 
| 329 | 
  | 
  | 
        int     eshft; | 
| 330 | 
  | 
  | 
        double  emult; | 
| 331 | 
  | 
  | 
        RESOLU  prs; | 
| 332 | 
schorsch | 
3.17 | 
        RREAL   vl[2]; | 
| 333 | 
gregl | 
3.1 | 
        FVECT   ro, rd; | 
| 334 | 
  | 
  | 
        double  aftd; | 
| 335 | 
  | 
  | 
        COLOR   ctmp; | 
| 336 | 
  | 
  | 
        int     j; | 
| 337 | 
  | 
  | 
        register int    i; | 
| 338 | 
  | 
  | 
                                /* open files */ | 
| 339 | 
  | 
  | 
        if ((pfp = fopen(pcf, "r")) == NULL) { | 
| 340 | 
  | 
  | 
                sprintf(errmsg, "cannot open picture file \"%s\"", pcf); | 
| 341 | 
  | 
  | 
                error(SYSTEM, pcf); | 
| 342 | 
  | 
  | 
        } | 
| 343 | 
  | 
  | 
        if ((zfd = open(zbf, O_RDONLY)) < 0) { | 
| 344 | 
  | 
  | 
                sprintf(errmsg, "cannot open depth file \"%s\"", zbf); | 
| 345 | 
  | 
  | 
                error(SYSTEM, pcf); | 
| 346 | 
  | 
  | 
        } | 
| 347 | 
  | 
  | 
                                /* load picture header */ | 
| 348 | 
schorsch | 
3.18 | 
        phd.vw = stdview; | 
| 349 | 
gregl | 
3.1 | 
        phd.expos = 1.0; | 
| 350 | 
  | 
  | 
        phd.badfmt = phd.gotview = phd.altprims = 0; | 
| 351 | 
schorsch | 
3.23 | 
        if (getheader(pfp, picheadline, &phd) < 0 || | 
| 352 | 
gregl | 
3.1 | 
                        phd.badfmt || !fgetsresolu(&prs, pfp)) { | 
| 353 | 
  | 
  | 
                sprintf(errmsg, "bad format for picture file \"%s\"", pcf); | 
| 354 | 
  | 
  | 
                error(USER, errmsg); | 
| 355 | 
  | 
  | 
        } | 
| 356 | 
  | 
  | 
        if (!phd.gotview || setview(&phd.vw) != NULL) { | 
| 357 | 
  | 
  | 
                sprintf(errmsg, "missing/illegal view in picture \"%s\"", | 
| 358 | 
  | 
  | 
                                pcf); | 
| 359 | 
  | 
  | 
                error(USER, errmsg); | 
| 360 | 
  | 
  | 
        } | 
| 361 | 
  | 
  | 
        if (phd.altprims) { | 
| 362 | 
greg | 
3.28 | 
                sprintf(errmsg, "ignoring color primaries in picture \"%s\"", | 
| 363 | 
gregl | 
3.1 | 
                                pcf); | 
| 364 | 
  | 
  | 
                error(WARNING, errmsg); | 
| 365 | 
  | 
  | 
        } | 
| 366 | 
  | 
  | 
                                /* figure out what to do about exposure */ | 
| 367 | 
schorsch | 
3.19 | 
        if ((phd.expos < 0.99) | (phd.expos > 1.01)) { | 
| 368 | 
gregl | 
3.1 | 
                emult = -log(phd.expos)/log(2.); | 
| 369 | 
  | 
  | 
                eshft = emult >= 0. ? emult+.5 : emult-.5; | 
| 370 | 
  | 
  | 
                emult -= (double)eshft; | 
| 371 | 
schorsch | 
3.19 | 
                if ((emult <= 0.01) & (emult >= -0.01)) | 
| 372 | 
gregl | 
3.1 | 
                        emult = -1.; | 
| 373 | 
  | 
  | 
                else { | 
| 374 | 
  | 
  | 
                        emult = 1./phd.expos; | 
| 375 | 
  | 
  | 
                        eshft = 0; | 
| 376 | 
  | 
  | 
                } | 
| 377 | 
  | 
  | 
        } else { | 
| 378 | 
  | 
  | 
                emult = -1.; | 
| 379 | 
  | 
  | 
                eshft = 0; | 
| 380 | 
  | 
  | 
        } | 
| 381 | 
  | 
  | 
                                /* allocate buffers */ | 
| 382 | 
  | 
  | 
        cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR)); | 
| 383 | 
  | 
  | 
        zscn = (float *)malloc(scanlen(&prs)*sizeof(float)); | 
| 384 | 
schorsch | 
3.19 | 
        if ((cscn == NULL) | (zscn == NULL)) | 
| 385 | 
gregl | 
3.1 | 
                error(SYSTEM, "out of memory in addpicz"); | 
| 386 | 
  | 
  | 
                                /* read and process each scanline */ | 
| 387 | 
  | 
  | 
        for (j = 0; j < numscans(&prs); j++) { | 
| 388 | 
  | 
  | 
                i = scanlen(&prs);                      /* read colrs */ | 
| 389 | 
  | 
  | 
                if (freadcolrs(cscn, i, pfp) < 0) { | 
| 390 | 
  | 
  | 
                        sprintf(errmsg, "error reading picture \"%s\"", pcf); | 
| 391 | 
  | 
  | 
                        error(USER, errmsg); | 
| 392 | 
  | 
  | 
                } | 
| 393 | 
  | 
  | 
                if (eshft)                              /* shift exposure */ | 
| 394 | 
  | 
  | 
                        shiftcolrs(cscn, i, eshft); | 
| 395 | 
  | 
  | 
                i *= sizeof(float);                     /* read depth */ | 
| 396 | 
  | 
  | 
                if (read(zfd, (char *)zscn, i) != i) { | 
| 397 | 
  | 
  | 
                        sprintf(errmsg, "error reading depth file \"%s\"", zbf); | 
| 398 | 
  | 
  | 
                        error(USER, errmsg); | 
| 399 | 
  | 
  | 
                } | 
| 400 | 
  | 
  | 
                for (i = scanlen(&prs); i--; ) {        /* do each pixel */ | 
| 401 | 
greg | 
3.29 | 
                        if (zscn[i] <= 0.0) | 
| 402 | 
  | 
  | 
                                continue;               /* illegal depth */ | 
| 403 | 
gregl | 
3.1 | 
                        pix2loc(vl, &prs, i, j); | 
| 404 | 
  | 
  | 
                        aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]); | 
| 405 | 
  | 
  | 
                        if (aftd < -FTINY) | 
| 406 | 
  | 
  | 
                                continue;               /* off view */ | 
| 407 | 
  | 
  | 
                        if (aftd > FTINY && zscn[i] > aftd) | 
| 408 | 
  | 
  | 
                                continue;               /* aft clipped */ | 
| 409 | 
  | 
  | 
                        if (emult > 0.) {               /* whatta pain */ | 
| 410 | 
  | 
  | 
                                colr_color(ctmp, cscn[i]); | 
| 411 | 
  | 
  | 
                                scalecolor(ctmp, emult); | 
| 412 | 
  | 
  | 
                                setcolr(cscn[i], colval(ctmp,RED), | 
| 413 | 
  | 
  | 
                                        colval(ctmp,GRN), colval(ctmp,BLU)); | 
| 414 | 
  | 
  | 
                        } | 
| 415 | 
  | 
  | 
                        addray(ro, rd, (double)zscn[i], cscn[i]); | 
| 416 | 
  | 
  | 
                } | 
| 417 | 
  | 
  | 
        } | 
| 418 | 
gwlarson | 
3.11 | 
                                /* write output and free beams */ | 
| 419 | 
  | 
  | 
        hdflush(NULL); | 
| 420 | 
gregl | 
3.1 | 
                                /* clean up */ | 
| 421 | 
greg | 
3.15 | 
        free((void *)cscn); | 
| 422 | 
  | 
  | 
        free((void *)zscn); | 
| 423 | 
gregl | 
3.1 | 
        fclose(pfp); | 
| 424 | 
  | 
  | 
        close(zfd); | 
| 425 | 
  | 
  | 
} | 
| 426 | 
  | 
  | 
 | 
| 427 | 
  | 
  | 
 | 
| 428 | 
greg | 
3.15 | 
void | 
| 429 | 
schorsch | 
3.22 | 
eputs(                  /* put error message to stderr */ | 
| 430 | 
  | 
  | 
        register char  *s | 
| 431 | 
  | 
  | 
) | 
| 432 | 
gregl | 
3.1 | 
{ | 
| 433 | 
  | 
  | 
        static int  midline = 0; | 
| 434 | 
  | 
  | 
 | 
| 435 | 
  | 
  | 
        if (!*s) | 
| 436 | 
  | 
  | 
                return; | 
| 437 | 
  | 
  | 
        if (!midline++) {       /* prepend line with program name */ | 
| 438 | 
  | 
  | 
                fputs(progname, stderr); | 
| 439 | 
  | 
  | 
                fputs(": ", stderr); | 
| 440 | 
  | 
  | 
        } | 
| 441 | 
  | 
  | 
        fputs(s, stderr); | 
| 442 | 
  | 
  | 
        if (s[strlen(s)-1] == '\n') { | 
| 443 | 
  | 
  | 
                fflush(stderr); | 
| 444 | 
  | 
  | 
                midline = 0; | 
| 445 | 
  | 
  | 
        } | 
| 446 | 
  | 
  | 
} | 
| 447 | 
  | 
  | 
 | 
| 448 | 
  | 
  | 
 | 
| 449 | 
greg | 
3.15 | 
void | 
| 450 | 
schorsch | 
3.22 | 
quit(                   /* exit the program gracefully */ | 
| 451 | 
  | 
  | 
        int     code | 
| 452 | 
  | 
  | 
) | 
| 453 | 
gregl | 
3.1 | 
{ | 
| 454 | 
  | 
  | 
        hdsync(NULL, 1);        /* write out any buffered data */ | 
| 455 | 
  | 
  | 
        exit(code); | 
| 456 | 
  | 
  | 
} |