ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/depthcodec.c
Revision: 2.6
Committed: Fri Jan 10 01:02:14 2020 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +5 -2 lines
Log Message:
Added check for 360-degree spherical sampling (unusable view)

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: depthcodec.c,v 2.5 2019/11/07 23:20:28 greg Exp $";
3 #endif
4 /*
5 * Routines to encode/decoded 16-bit depths
6 */
7
8 #include "copyright.h"
9
10 #include <stdlib.h>
11 #include <math.h>
12 #include <ctype.h>
13 #include "rtio.h"
14 #include "fvect.h"
15 #include "depthcodec.h"
16
17
18 #ifndef depth2code
19 /* Encode depth as 16-bit signed integer */
20 int
21 depth2code(double d, double dref)
22 {
23 if (d <= .0)
24 return -32768;
25
26 if (d > dref)
27 return (int)(32768.001 - 32768.*dref/d) - 1;
28
29 return (int)(32767.*d/dref - 32768.);
30 }
31 #endif
32
33
34 #ifndef code2depth
35 /* Decode depth from 16-bit signed integer */
36 double
37 code2depth(int c, double dref)
38 {
39 if (c <= -32768)
40 return .0;
41
42 if (c >= 32767)
43 return FHUGE;
44
45 if (c >= 0)
46 return dref*32768./(32766.5 - c);
47
48 return dref*(32767.5 + c)*(1./32767.);
49 }
50 #endif
51
52
53 /* Set codec defaults */
54 void
55 set_dc_defaults(DEPTHCODEC *dcp)
56 {
57 memset(dcp, 0, sizeof(DEPTHCODEC));
58 dcp->finp = stdin;
59 dcp->inpname = "<stdin>";
60 dcp->format = 'a';
61 dcp->refdepth = 1.;
62 dcp->depth_unit[0] = '1';
63 dcp->vw = stdview;
64 dcp->res.rt = PIXSTANDARD;
65 if (!progname) progname = "depth_codec";
66 }
67
68
69 /* process header line */
70 static int
71 headline(char *s, void *p)
72 {
73 DEPTHCODEC *dcp = (DEPTHCODEC *)p;
74 int rv;
75
76 if (formatval(dcp->inpfmt, s)) /* don't pass format */
77 return 0;
78
79 if ((rv = isbigendian(s)) >= 0) {
80 dcp->swapped = (nativebigendian() != rv);
81 return 0;
82 }
83 /* check for reference depth */
84 if (!strncmp(s, DEPTHSTR, LDEPTHSTR)) {
85 char *cp;
86 strlcpy(dcp->depth_unit, s+LDEPTHSTR, sizeof(dcp->depth_unit));
87 cp = dcp->depth_unit;
88 while (*cp) cp++;
89 while (cp > dcp->depth_unit && isspace(cp[-1])) cp--;
90 *cp = '\0';
91 dcp->refdepth = atof(dcp->depth_unit);
92 if (dcp->refdepth <= .0) {
93 if (dcp->hdrflags & HF_STDERR) {
94 fputs(dcp->inpname, stderr);
95 fputs(": bad reference depth in input header\n",
96 stderr);
97 }
98 return -1;
99 }
100 } else if (!strncmp(s, "SAMP360=", 8))
101 dcp->gotview--;
102 else if (isview(s)) /* get view params */
103 dcp->gotview += (sscanview(&dcp->vw, s) > 0);
104 if (dcp->hdrflags & HF_HEADOUT)
105 fputs(s, stdout); /* copy to standard output */
106 return 1;
107 }
108
109
110 /* Load/copy header */
111 int
112 process_dc_header(DEPTHCODEC *dcp, int ac, char *av[])
113 {
114 if (dcp->hdrflags & HF_HEADIN &&
115 getheader(dcp->finp, headline, dcp) < 0) {
116 if (dcp->hdrflags & HF_STDERR) {
117 fputs(dcp->inpname, stderr);
118 fputs(": bad header\n", stderr);
119 }
120 return 0;
121 }
122 dcp->gotview *= (dcp->gotview > 0);
123 if (dcp->hdrflags & HF_HEADOUT) { /* finish header */
124 if (!(dcp->hdrflags & HF_HEADIN))
125 newheader("RADIANCE", stdout);
126 if (ac > 0)
127 printargs(ac, av, stdout);
128 if (dcp->hdrflags & HF_ENCODE) {
129 fputs(DEPTHSTR, stdout);
130 fputs(dcp->depth_unit, stdout);
131 fputc('\n', stdout);
132 fputformat(DEPTH16FMT, stdout);
133 } else
134 switch (dcp->format) {
135 case 'a':
136 fputformat("ascii", stdout);
137 break;
138 case 'f':
139 fputendian(stdout);
140 fputformat("float", stdout);
141 break;
142 case 'd':
143 fputendian(stdout);
144 fputformat("double", stdout);
145 break;
146 }
147 fputc('\n', stdout);
148 }
149 /* get/put resolution string */
150 if (dcp->hdrflags & HF_RESIN && !fgetsresolu(&dcp->res, dcp->finp)) {
151 if (dcp->hdrflags & HF_STDERR) {
152 fputs(dcp->inpname, stderr);
153 fputs(": bad resolution string\n", stderr);
154 }
155 return 0;
156 }
157 if (dcp->hdrflags & HF_RESOUT)
158 fputsresolu(&dcp->res, stdout);
159
160 dcp->dstart = dcp->curpos = ftell(dcp->finp);
161 return 1;
162 }
163
164
165 /* Check that we have what we need to decode depths */
166 int
167 check_decode_depths(DEPTHCODEC *dcp)
168 {
169 if (dcp->hdrflags & HF_ENCODE) {
170 if (dcp->hdrflags & HF_STDERR) {
171 fputs(progname, stderr);
172 fputs(": wrong header mode for decode\n", stderr);
173 }
174 return 0;
175 }
176 if (dcp->inpfmt[0] && strcmp(dcp->inpfmt, DEPTH16FMT)) {
177 if (dcp->hdrflags & HF_STDERR) {
178 fputs(dcp->inpname, stderr);
179 fputs(": unexpected input format: ", stderr);
180 fputs(dcp->inpfmt, stderr);
181 fputc('\n', stderr);
182 }
183 return 0;
184 }
185 return 1;
186 }
187
188
189 /* Check that we have what we need to decode world positions */
190 int
191 check_decode_worldpos(DEPTHCODEC *dcp)
192 {
193 char *err;
194
195 if (!check_decode_depths(dcp))
196 return 0;
197 if ((dcp->res.xr <= 0) | (dcp->res.yr <= 0)) {
198 if (dcp->hdrflags & HF_STDERR) {
199 fputs(progname, stderr);
200 fputs(": missing map resolution\n", stderr);
201 }
202 return 0;
203 }
204 if (!dcp->gotview) {
205 if (dcp->hdrflags & HF_STDERR) {
206 fputs(dcp->inpname, stderr);
207 fputs(": missing view\n", stderr);
208 }
209 return 0;
210 }
211 if ((err = setview(&dcp->vw)) != NULL) {
212 if (dcp->hdrflags & HF_STDERR) {
213 fputs(dcp->inpname, stderr);
214 fputs(": input view error: ", stderr);
215 fputs(err, stderr);
216 fputc('\n', stderr);
217 }
218 return 0;
219 }
220 return 1;
221 }
222
223
224 /* Decode next depth pixel */
225 double
226 decode_depth_next(DEPTHCODEC *dcp)
227 {
228 int c;
229
230 if (dcp->use_last) {
231 dcp->use_last = 0;
232 return code2depth(dcp->last_dc, dcp->refdepth);
233 }
234 c = getint(2, dcp->finp);
235
236 if (c == EOF && feof(dcp->finp))
237 return -1.;
238
239 dcp->last_dc = c;
240 dcp->curpos += 2;
241
242 return code2depth(c, dcp->refdepth);
243 }
244
245
246 /* Compute world position from depth */
247 int
248 compute_worldpos(FVECT wpos, DEPTHCODEC *dcp, int x, int y, double d)
249 {
250 RREAL loc[2];
251 FVECT rdir;
252
253 pix2loc(loc, &dcp->res, x, y);
254
255 if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) < -FTINY) {
256 VCOPY(wpos, dcp->vw.vp);
257 return 0;
258 }
259 VSUM(wpos, wpos, rdir, d);
260 return 1;
261 }
262
263
264 /* Decode the next world position */
265 int
266 decode_worldpos_next(FVECT wpos, DEPTHCODEC *dcp)
267 {
268 const long n = (dcp->curpos - dcp->dstart)>>1;
269 int x, y;
270 double d;
271
272 d = decode_depth_next(dcp);
273 if (d < -FTINY)
274 return -1;
275
276 x = scanlen(&dcp->res);
277 y = n / x;
278 x = n - (long)y*x;
279
280 return compute_worldpos(wpos, dcp, x, y, d);
281 }
282
283
284 /* Seek to the indicated pixel position */
285 int
286 seek_dc_pix(DEPTHCODEC *dcp, int x, int y)
287 {
288 long seekpos;
289
290 if ((dcp->res.xr <= 0) | (dcp->res.yr <= 0)) {
291 if (dcp->hdrflags & HF_STDERR) {
292 fputs(progname, stderr);
293 fputs(": need map resolution to seek\n", stderr);
294 }
295 return -1;
296 }
297 if ((x < 0) | (y < 0) ||
298 (x >= scanlen(&dcp->res)) | (y >= numscans(&dcp->res))) {
299 if (dcp->hdrflags & HF_STDERR) {
300 fputs(dcp->inpname, stderr);
301 fputs(": warning - pixel index off map\n", stderr);
302 }
303 return 0;
304 }
305 seekpos = dcp->dstart + 2*((long)y*scanlen(&dcp->res) + x);
306
307 if (seekpos == dcp->curpos-2) {
308 dcp->use_last++; /* avoids seek/read */
309 return 1;
310 }
311 if (seekpos != dcp->curpos &&
312 fseek(dcp->finp, seekpos, SEEK_SET) == EOF) {
313 if (dcp->hdrflags & HF_STDERR) {
314 fputs(dcp->inpname, stderr);
315 fputs(": seek error\n", stderr);
316 }
317 return -1;
318 }
319 dcp->curpos = seekpos;
320 return 1;
321 }
322
323
324 /* Read and decode depth for the given pixel */
325 double
326 decode_depth_pix(DEPTHCODEC *dcp, int x, int y)
327 {
328 int rval = seek_dc_pix(dcp, x, y);
329
330 if (rval < 0)
331 return -1.;
332 if (!rval)
333 return .0;
334
335 return decode_depth_next(dcp);
336 }
337
338
339 /* Read and decode the world position at the given pixel */
340 int
341 get_worldpos_pix(FVECT wpos, DEPTHCODEC *dcp, int x, int y)
342 {
343 double d = decode_depth_pix(dcp, x, y);
344
345 if (d < -FTINY)
346 return -1;
347
348 return compute_worldpos(wpos, dcp, x, y, d);
349 }