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