| 1 | 
greg | 
3.2 | 
#ifndef lint | 
| 2 | 
greg | 
3.4 | 
static const char RCSid[] = "$Id: fltdepth.c,v 3.3 2019/11/08 17:08:21 greg Exp $"; | 
| 3 | 
greg | 
3.2 | 
#endif | 
| 4 | 
greg | 
3.1 | 
/* | 
| 5 | 
  | 
  | 
 * Function to open floating-point depth file, making sure it's | 
| 6 | 
  | 
  | 
 * the correct length, and converting from encoded 16-bit | 
| 7 | 
  | 
  | 
 * depth into a temporary file if needed | 
| 8 | 
  | 
  | 
 */ | 
| 9 | 
  | 
  | 
 | 
| 10 | 
  | 
  | 
#include "rtio.h" | 
| 11 | 
  | 
  | 
#include "paths.h" | 
| 12 | 
  | 
  | 
 | 
| 13 | 
  | 
  | 
#if defined(_WIN32) || defined(_WIN64) | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
#include "platform.h" | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
/* Return file descriptor for open raw float file, or -1 on error */ | 
| 18 | 
  | 
  | 
/* Set expected_length to width*height, or 0 if unknown */ | 
| 19 | 
  | 
  | 
/* We don't translate encoded depth under Windows because of temp file issue */ | 
| 20 | 
  | 
  | 
int | 
| 21 | 
  | 
  | 
open_float_depth(const char *fname, long expected_length) | 
| 22 | 
  | 
  | 
{ | 
| 23 | 
  | 
  | 
        int     fd = open(fname, O_RDONLY|O_BINARY); | 
| 24 | 
  | 
  | 
 | 
| 25 | 
greg | 
3.4 | 
        if (fd < 0) { | 
| 26 | 
  | 
  | 
                fprintf(stderr, "%s: cannot open for reading\n", fname); | 
| 27 | 
greg | 
3.1 | 
                return(-1); | 
| 28 | 
greg | 
3.4 | 
        } | 
| 29 | 
greg | 
3.1 | 
        if (expected_length > 0) { | 
| 30 | 
  | 
  | 
                off_t   flen = lseek(fd, 0, SEEK_END); | 
| 31 | 
  | 
  | 
                if (flen != expected_length*sizeof(float)) { | 
| 32 | 
  | 
  | 
                        fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n", | 
| 33 | 
  | 
  | 
                                        fname, expected_length, (long)(flen/sizeof(float))); | 
| 34 | 
  | 
  | 
                        close(fd); | 
| 35 | 
  | 
  | 
                        return(-1); | 
| 36 | 
  | 
  | 
                } | 
| 37 | 
  | 
  | 
                lseek(fd, 0, SEEK_SET); | 
| 38 | 
  | 
  | 
        } | 
| 39 | 
  | 
  | 
        return(fd); | 
| 40 | 
  | 
  | 
} | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
#else   /* ! defined(_WIN32) || defined(_WIN64) */ | 
| 43 | 
  | 
  | 
 | 
| 44 | 
  | 
  | 
#include <ctype.h> | 
| 45 | 
  | 
  | 
#include "fvect.h" | 
| 46 | 
  | 
  | 
#include "depthcodec.h" | 
| 47 | 
  | 
  | 
 | 
| 48 | 
  | 
  | 
#define FBUFLEN         1024            /* depth conversion batch size */ | 
| 49 | 
  | 
  | 
 | 
| 50 | 
  | 
  | 
/* Return file descriptor for open raw float file, or -1 on error */ | 
| 51 | 
  | 
  | 
/* Set expected_length to width*height, or 0 if unknown */ | 
| 52 | 
  | 
  | 
int | 
| 53 | 
  | 
  | 
open_float_depth(const char *fname, long expected_length) | 
| 54 | 
  | 
  | 
{ | 
| 55 | 
  | 
  | 
        char            buf[FBUFLEN*sizeof(float)]; | 
| 56 | 
  | 
  | 
        DEPTHCODEC      dc; | 
| 57 | 
  | 
  | 
        ssize_t         n, nleft; | 
| 58 | 
  | 
  | 
        int             fd = open(fname, O_RDONLY); | 
| 59 | 
  | 
  | 
 | 
| 60 | 
greg | 
3.4 | 
        if (fd < 0) { | 
| 61 | 
  | 
  | 
                perror(fname); | 
| 62 | 
greg | 
3.1 | 
                return(-1); | 
| 63 | 
greg | 
3.4 | 
        } | 
| 64 | 
greg | 
3.1 | 
        dc.finp = NULL; | 
| 65 | 
  | 
  | 
        if (expected_length <= 0) {     /* need to sniff file? */ | 
| 66 | 
  | 
  | 
                extern const char       HDRSTR[]; | 
| 67 | 
  | 
  | 
                const int               len = strlen(HDRSTR); | 
| 68 | 
greg | 
3.3 | 
                if (read(fd, buf, len+1) < len+1) | 
| 69 | 
greg | 
3.4 | 
                        goto badEOF; | 
| 70 | 
greg | 
3.1 | 
                if (lseek(fd, 0, SEEK_SET) != 0) | 
| 71 | 
  | 
  | 
                        goto seek_error; | 
| 72 | 
  | 
  | 
                for (n = 0; n < len; n++) | 
| 73 | 
  | 
  | 
                        if (buf[n] != HDRSTR[n]) | 
| 74 | 
  | 
  | 
                                break; | 
| 75 | 
greg | 
3.3 | 
                if ((n < len) | !isprint(buf[len]))      | 
| 76 | 
greg | 
3.1 | 
                        return(fd);     /* unknown length raw float... */ | 
| 77 | 
  | 
  | 
        } else { | 
| 78 | 
  | 
  | 
                off_t   flen = lseek(fd, 0, SEEK_END); | 
| 79 | 
  | 
  | 
                if (flen < 0 || lseek(fd, 0, SEEK_SET) != 0) | 
| 80 | 
  | 
  | 
                        goto seek_error; | 
| 81 | 
  | 
  | 
                if (flen == expected_length*sizeof(float)) | 
| 82 | 
  | 
  | 
                        return(fd);     /* it's the right length for raw */ | 
| 83 | 
  | 
  | 
        } | 
| 84 | 
  | 
  | 
        /* We're thinking it's an encoded depth file at this point... */ | 
| 85 | 
  | 
  | 
        set_dc_defaults(&dc);           /* load & convert if we can */ | 
| 86 | 
  | 
  | 
        dc.finp = fdopen(fd, "r"); | 
| 87 | 
  | 
  | 
        if (!dc.finp) {                 /* out of memory?? */ | 
| 88 | 
  | 
  | 
                perror("fdopen"); | 
| 89 | 
  | 
  | 
                close(fd); | 
| 90 | 
  | 
  | 
                return(-1); | 
| 91 | 
  | 
  | 
        } | 
| 92 | 
  | 
  | 
        dc.inpname = fname;             /* check encoded depth header */ | 
| 93 | 
  | 
  | 
        dc.hdrflags = HF_HEADIN|HF_RESIN|HF_STDERR; | 
| 94 | 
  | 
  | 
        if (!process_dc_header(&dc, 0, NULL) || !check_decode_depths(&dc)) { | 
| 95 | 
  | 
  | 
                fclose(dc.finp);        /* already reported error */ | 
| 96 | 
  | 
  | 
                return(-1); | 
| 97 | 
  | 
  | 
        } | 
| 98 | 
greg | 
3.3 | 
        if ((expected_length > 0) &  | 
| 99 | 
  | 
  | 
                        ((long)dc.res.xr*dc.res.yr != expected_length)) { | 
| 100 | 
greg | 
3.1 | 
                fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n", | 
| 101 | 
greg | 
3.3 | 
                                fname, expected_length, (long)dc.res.xr*dc.res.yr); | 
| 102 | 
greg | 
3.1 | 
                fclose(dc.finp); | 
| 103 | 
  | 
  | 
                return(-1); | 
| 104 | 
  | 
  | 
        } | 
| 105 | 
  | 
  | 
        strcpy(buf, TEMPLATE);          /* create temporary file to hold raw */ | 
| 106 | 
  | 
  | 
        fd = mkstemp(buf); | 
| 107 | 
  | 
  | 
        if (fd < 0) { | 
| 108 | 
greg | 
3.3 | 
                perror(buf); | 
| 109 | 
greg | 
3.1 | 
                fclose(dc.finp); | 
| 110 | 
  | 
  | 
                return(-1); | 
| 111 | 
  | 
  | 
        } | 
| 112 | 
greg | 
3.3 | 
        unlink(buf);                    /* preemptive remove (Windows forbids) */ | 
| 113 | 
greg | 
3.1 | 
        for (nleft = (ssize_t)dc.res.xr*dc.res.yr; nleft > 0; nleft -= FBUFLEN) { | 
| 114 | 
  | 
  | 
                for (n = 0; n < FBUFLEN; n++) { | 
| 115 | 
  | 
  | 
                        double  d = decode_depth_next(&dc); | 
| 116 | 
  | 
  | 
                        if (d < 0) { | 
| 117 | 
  | 
  | 
                                if (n < nleft) | 
| 118 | 
greg | 
3.4 | 
                                        goto badEOF; | 
| 119 | 
greg | 
3.1 | 
                                break; | 
| 120 | 
  | 
  | 
                        } | 
| 121 | 
greg | 
3.3 | 
                        ((float *)buf)[n] = d; | 
| 122 | 
greg | 
3.1 | 
                } | 
| 123 | 
  | 
  | 
                n *= sizeof(float); | 
| 124 | 
  | 
  | 
                if (write(fd, buf, n) != n) { | 
| 125 | 
  | 
  | 
                        perror("write"); | 
| 126 | 
  | 
  | 
                        fclose(dc.finp); | 
| 127 | 
  | 
  | 
                        close(fd); | 
| 128 | 
  | 
  | 
                        return(-1); | 
| 129 | 
  | 
  | 
                } | 
| 130 | 
  | 
  | 
        } | 
| 131 | 
  | 
  | 
        fclose(dc.finp);                /* all done -- clean up */ | 
| 132 | 
  | 
  | 
        if (lseek(fd, 0, SEEK_SET) != 0) | 
| 133 | 
  | 
  | 
                goto seek_error; | 
| 134 | 
  | 
  | 
        return(fd); | 
| 135 | 
greg | 
3.4 | 
badEOF: | 
| 136 | 
greg | 
3.1 | 
        fputs(fname, stderr); | 
| 137 | 
  | 
  | 
        fputs(": unexpected end-of-file\n", stderr); | 
| 138 | 
  | 
  | 
        if (dc.finp) fclose(dc.finp); | 
| 139 | 
  | 
  | 
        close(fd); | 
| 140 | 
  | 
  | 
        return(-1); | 
| 141 | 
  | 
  | 
seek_error: | 
| 142 | 
  | 
  | 
        perror("lseek"); | 
| 143 | 
  | 
  | 
        close(fd); | 
| 144 | 
  | 
  | 
        return(-1); | 
| 145 | 
  | 
  | 
} | 
| 146 | 
  | 
  | 
 | 
| 147 | 
greg | 
3.2 | 
#endif |