1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: depthcodec.c,v 2.10 2020/03/05 17:43:22 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.999); |
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 >= -1) |
46 |
return dref*32768./(32766.5 - c); |
47 |
|
48 |
return dref*(32768.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 |
if (dcp->hdrflags & HF_ENCODE) |
101 |
return 0; /* will add this later */ |
102 |
} else if (!strncmp(s, "SAMP360=", 8)) |
103 |
dcp->gotview--; |
104 |
else if (isview(s)) /* get view params */ |
105 |
dcp->gotview += (sscanview(&dcp->vw, s) > 0); |
106 |
if (dcp->hdrflags & HF_HEADOUT) |
107 |
fputs(s, stdout); /* copy to standard output */ |
108 |
return 1; |
109 |
} |
110 |
|
111 |
|
112 |
/* Load/copy header */ |
113 |
int |
114 |
process_dc_header(DEPTHCODEC *dcp, int ac, char *av[]) |
115 |
{ |
116 |
if (dcp->hdrflags & HF_HEADIN && |
117 |
getheader(dcp->finp, headline, dcp) < 0) { |
118 |
if (dcp->hdrflags & HF_STDERR) { |
119 |
fputs(dcp->inpname, stderr); |
120 |
fputs(": bad header\n", stderr); |
121 |
} |
122 |
return 0; |
123 |
} |
124 |
dcp->gotview *= (dcp->gotview > 0); |
125 |
if (dcp->hdrflags & HF_HEADOUT) { /* finish header */ |
126 |
if (!(dcp->hdrflags & HF_HEADIN)) |
127 |
newheader("RADIANCE", stdout); |
128 |
if (ac > 0) |
129 |
printargs(ac, av, stdout); |
130 |
if (dcp->hdrflags & HF_ENCODE) { |
131 |
fputs(DEPTHSTR, stdout); |
132 |
fputs(dcp->depth_unit, stdout); |
133 |
fputc('\n', stdout); |
134 |
fputformat(DEPTH16FMT, stdout); |
135 |
} else |
136 |
switch (dcp->format) { |
137 |
case 'a': |
138 |
fputformat("ascii", stdout); |
139 |
break; |
140 |
case 'f': |
141 |
fputendian(stdout); |
142 |
fputformat("float", stdout); |
143 |
break; |
144 |
case 'd': |
145 |
fputendian(stdout); |
146 |
fputformat("double", stdout); |
147 |
break; |
148 |
} |
149 |
fputc('\n', stdout); |
150 |
} |
151 |
/* get/put resolution string */ |
152 |
if (dcp->hdrflags & HF_RESIN && !fgetsresolu(&dcp->res, dcp->finp)) { |
153 |
if (dcp->hdrflags & HF_STDERR) { |
154 |
fputs(dcp->inpname, stderr); |
155 |
fputs(": bad resolution string\n", stderr); |
156 |
} |
157 |
return 0; |
158 |
} |
159 |
if (dcp->hdrflags & HF_RESOUT) |
160 |
fputsresolu(&dcp->res, stdout); |
161 |
|
162 |
dcp->dstart = dcp->curpos = ftell(dcp->finp); |
163 |
return 1; |
164 |
} |
165 |
|
166 |
|
167 |
/* Check that we have what we need to decode depths */ |
168 |
int |
169 |
check_decode_depths(DEPTHCODEC *dcp) |
170 |
{ |
171 |
if (dcp->hdrflags & HF_ENCODE) { |
172 |
if (dcp->hdrflags & HF_STDERR) { |
173 |
fputs(progname, stderr); |
174 |
fputs(": wrong header mode for decode\n", stderr); |
175 |
} |
176 |
return 0; |
177 |
} |
178 |
if (dcp->inpfmt[0] && strcmp(dcp->inpfmt, DEPTH16FMT)) { |
179 |
if (dcp->hdrflags & HF_STDERR) { |
180 |
fputs(dcp->inpname, stderr); |
181 |
fputs(": unexpected input format: ", stderr); |
182 |
fputs(dcp->inpfmt, stderr); |
183 |
fputc('\n', stderr); |
184 |
} |
185 |
return 0; |
186 |
} |
187 |
return 1; |
188 |
} |
189 |
|
190 |
|
191 |
/* Check that we have what we need to decode world positions */ |
192 |
int |
193 |
check_decode_worldpos(DEPTHCODEC *dcp) |
194 |
{ |
195 |
char *err; |
196 |
|
197 |
if (!check_decode_depths(dcp)) |
198 |
return 0; |
199 |
if ((dcp->res.xr <= 0) | (dcp->res.yr <= 0)) { |
200 |
if (dcp->hdrflags & HF_STDERR) { |
201 |
fputs(progname, stderr); |
202 |
fputs(": missing map resolution\n", stderr); |
203 |
} |
204 |
return 0; |
205 |
} |
206 |
if (!dcp->gotview) { |
207 |
if (dcp->hdrflags & HF_STDERR) { |
208 |
fputs(dcp->inpname, stderr); |
209 |
fputs(": missing view\n", stderr); |
210 |
} |
211 |
return 0; |
212 |
} |
213 |
if ((err = setview(&dcp->vw)) != NULL) { |
214 |
if (dcp->hdrflags & HF_STDERR) { |
215 |
fputs(dcp->inpname, stderr); |
216 |
fputs(": input view error: ", stderr); |
217 |
fputs(err, stderr); |
218 |
fputc('\n', stderr); |
219 |
} |
220 |
return 0; |
221 |
} |
222 |
return 1; |
223 |
} |
224 |
|
225 |
|
226 |
/* Decode next depth pixel */ |
227 |
double |
228 |
decode_depth_next(DEPTHCODEC *dcp) |
229 |
{ |
230 |
int c; |
231 |
|
232 |
if (dcp->use_last) { |
233 |
dcp->use_last = 0; |
234 |
return code2depth(dcp->last_dc, dcp->refdepth); |
235 |
} |
236 |
c = getint(2, dcp->finp); |
237 |
|
238 |
if (c == EOF && feof(dcp->finp)) |
239 |
return -1.; |
240 |
|
241 |
dcp->last_dc = c; |
242 |
dcp->curpos += 2; |
243 |
|
244 |
return code2depth(c, dcp->refdepth); |
245 |
} |
246 |
|
247 |
|
248 |
/* Compute world position from depth */ |
249 |
int |
250 |
compute_worldpos(FVECT wpos, DEPTHCODEC *dcp, int x, int y, double d) |
251 |
{ |
252 |
RREAL loc[2]; |
253 |
FVECT rdir; |
254 |
|
255 |
if (d >= FHUGE*.99) |
256 |
goto badval; |
257 |
|
258 |
pix2loc(loc, &dcp->res, x, y); |
259 |
|
260 |
if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) >= -FTINY) { |
261 |
VSUM(wpos, wpos, rdir, d); |
262 |
return 1; |
263 |
} |
264 |
badval: |
265 |
VCOPY(wpos, dcp->vw.vp); |
266 |
return 0; |
267 |
} |
268 |
|
269 |
|
270 |
/* Decode the next world position */ |
271 |
int |
272 |
decode_worldpos_next(FVECT wpos, DEPTHCODEC *dcp) |
273 |
{ |
274 |
const long n = (dcp->curpos - dcp->dstart)>>1; |
275 |
int x, y; |
276 |
double d; |
277 |
|
278 |
d = decode_depth_next(dcp); |
279 |
if (d < -FTINY) |
280 |
return -1; |
281 |
|
282 |
x = scanlen(&dcp->res); |
283 |
y = n / x; |
284 |
x = n - (long)y*x; |
285 |
|
286 |
return compute_worldpos(wpos, dcp, x, y, d); |
287 |
} |
288 |
|
289 |
|
290 |
/* Seek to the indicated pixel position */ |
291 |
int |
292 |
seek_dc_pix(DEPTHCODEC *dcp, int x, int y) |
293 |
{ |
294 |
long seekpos; |
295 |
|
296 |
if ((dcp->res.xr <= 0) | (dcp->res.yr <= 0)) { |
297 |
if (dcp->hdrflags & HF_STDERR) { |
298 |
fputs(progname, stderr); |
299 |
fputs(": need map resolution to seek\n", stderr); |
300 |
} |
301 |
return -1; |
302 |
} |
303 |
if ((x < 0) | (y < 0) || |
304 |
(x >= scanlen(&dcp->res)) | (y >= numscans(&dcp->res))) { |
305 |
if (dcp->hdrflags & HF_STDERR) { |
306 |
fputs(dcp->inpname, stderr); |
307 |
fputs(": warning - pixel index off map\n", stderr); |
308 |
} |
309 |
return 0; |
310 |
} |
311 |
seekpos = dcp->dstart + 2*((long)y*scanlen(&dcp->res) + x); |
312 |
|
313 |
if (seekpos == dcp->curpos-2) { |
314 |
dcp->use_last++; /* avoids seek/read */ |
315 |
return 1; |
316 |
} |
317 |
if (seekpos != dcp->curpos && |
318 |
fseek(dcp->finp, seekpos, SEEK_SET) == EOF) { |
319 |
if (dcp->hdrflags & HF_STDERR) { |
320 |
fputs(dcp->inpname, stderr); |
321 |
fputs(": seek error\n", stderr); |
322 |
} |
323 |
return -1; |
324 |
} |
325 |
dcp->curpos = seekpos; |
326 |
dcp->use_last = 0; |
327 |
return 1; |
328 |
} |
329 |
|
330 |
|
331 |
/* Read and decode depth for the given pixel */ |
332 |
double |
333 |
decode_depth_pix(DEPTHCODEC *dcp, int x, int y) |
334 |
{ |
335 |
int rval = seek_dc_pix(dcp, x, y); |
336 |
|
337 |
if (rval < 0) |
338 |
return -1.; |
339 |
if (!rval) |
340 |
return .0; |
341 |
|
342 |
return decode_depth_next(dcp); |
343 |
} |
344 |
|
345 |
|
346 |
/* Read and decode the world position at the given pixel */ |
347 |
int |
348 |
get_worldpos_pix(FVECT wpos, DEPTHCODEC *dcp, int x, int y) |
349 |
{ |
350 |
double d = decode_depth_pix(dcp, x, y); |
351 |
|
352 |
if (d < -FTINY) |
353 |
return -1; |
354 |
|
355 |
return compute_worldpos(wpos, dcp, x, y, d); |
356 |
} |