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