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