ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fltdepth.c
Revision: 3.5
Committed: Thu Mar 5 17:37:09 2020 UTC (4 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3, HEAD
Changes since 3.4: +8 -9 lines
Log Message:
Minor change should have no effect on results

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: fltdepth.c,v 3.4 2019/11/11 16:45:30 greg Exp $";
3 #endif
4 /*
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 fprintf(stderr, "%s: cannot open for reading\n", fname);
27 return(-1);
28 }
29 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 if (fd < 0) {
61 perror(fname);
62 return(-1);
63 }
64 dc.finp = NULL;
65 if (expected_length <= 0) { /* need to sniff file? */
66 extern const char HDRSTR[];
67 const int len = strlen(HDRSTR);
68 if (read(fd, buf, len+1) < len+1)
69 goto badEOF;
70 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 if ((n < len) | !isprint(buf[len]))
76 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 if ((expected_length > 0) &
99 ((long)dc.res.xr*dc.res.yr != expected_length)) {
100 fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n",
101 fname, expected_length, (long)dc.res.xr*dc.res.yr);
102 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 perror(buf);
109 fclose(dc.finp);
110 return(-1);
111 }
112 unlink(buf); /* preemptive remove (Windows forbids) */
113 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 < -FTINY) {
117 if (n < nleft)
118 goto badEOF;
119 break;
120 }
121 ((float *)buf)[n] = d;
122 }
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 return(fd);
134 seek_error:
135 perror("lseek");
136 close(fd);
137 return(-1);
138 badEOF:
139 fputs(fname, stderr);
140 fputs(": unexpected end-of-file\n", stderr);
141 if (dc.finp) fclose(dc.finp);
142 close(fd);
143 return(-1);
144 }
145
146 #endif