ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fltdepth.c
Revision: 3.3
Committed: Fri Nov 8 17:08:21 2019 UTC (4 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.2: +9 -10 lines
Log Message:
Minor changes should not affect behavior

File Contents

# User Rev Content
1 greg 3.2 #ifndef lint
2 greg 3.3 static const char RCSid[] = "$Id: fltdepth.c,v 3.2 2019/11/07 23:33:45 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     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 greg 3.3 if (read(fd, buf, len+1) < len+1)
65 greg 3.1 goto gotEOF;
66     if (lseek(fd, 0, SEEK_SET) != 0)
67     goto seek_error;
68     for (n = 0; n < len; n++)
69     if (buf[n] != HDRSTR[n])
70     break;
71 greg 3.3 if ((n < len) | !isprint(buf[len]))
72 greg 3.1 return(fd); /* unknown length raw float... */
73     } else {
74     off_t flen = lseek(fd, 0, SEEK_END);
75     if (flen < 0 || lseek(fd, 0, SEEK_SET) != 0)
76     goto seek_error;
77     if (flen == expected_length*sizeof(float))
78     return(fd); /* it's the right length for raw */
79     }
80     /* We're thinking it's an encoded depth file at this point... */
81     set_dc_defaults(&dc); /* load & convert if we can */
82     dc.finp = fdopen(fd, "r");
83     if (!dc.finp) { /* out of memory?? */
84     perror("fdopen");
85     close(fd);
86     return(-1);
87     }
88     dc.inpname = fname; /* check encoded depth header */
89     dc.hdrflags = HF_HEADIN|HF_RESIN|HF_STDERR;
90     if (!process_dc_header(&dc, 0, NULL) || !check_decode_depths(&dc)) {
91     fclose(dc.finp); /* already reported error */
92     return(-1);
93     }
94 greg 3.3 if ((expected_length > 0) &
95     ((long)dc.res.xr*dc.res.yr != expected_length)) {
96 greg 3.1 fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n",
97 greg 3.3 fname, expected_length, (long)dc.res.xr*dc.res.yr);
98 greg 3.1 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 greg 3.3 perror(buf);
105 greg 3.1 fclose(dc.finp);
106     return(-1);
107     }
108 greg 3.3 unlink(buf); /* preemptive remove (Windows forbids) */
109 greg 3.1 for (nleft = (ssize_t)dc.res.xr*dc.res.yr; nleft > 0; nleft -= FBUFLEN) {
110     for (n = 0; n < FBUFLEN; n++) {
111     double d = decode_depth_next(&dc);
112     if (d < 0) {
113     if (n < nleft)
114     goto gotEOF;
115     break;
116     }
117 greg 3.3 ((float *)buf)[n] = d;
118 greg 3.1 }
119     n *= sizeof(float);
120     if (write(fd, buf, n) != n) {
121     perror("write");
122     fclose(dc.finp);
123     close(fd);
124     return(-1);
125     }
126     }
127     fclose(dc.finp); /* all done -- clean up */
128     if (lseek(fd, 0, SEEK_SET) != 0)
129     goto seek_error;
130     return(fd);
131     gotEOF:
132     fputs(fname, stderr);
133     fputs(": unexpected end-of-file\n", stderr);
134     if (dc.finp) fclose(dc.finp);
135     close(fd);
136     return(-1);
137     seek_error:
138     perror("lseek");
139     close(fd);
140     return(-1);
141     }
142    
143 greg 3.2 #endif