2 |
|
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
< |
* Encode and decode depth values using 16-bit integers |
5 |
> |
* Encode and decode depth map using 16-bit integers |
6 |
|
*/ |
7 |
|
|
8 |
|
#include "copyright.h" |
15 |
|
#include "fvect.h" |
16 |
|
#include "depthcodec.h" |
17 |
|
|
18 |
+ |
char *progname; /* set in main() */ |
19 |
+ |
|
20 |
|
enum {CV_FWD, CV_REV, CV_PTS}; |
21 |
|
|
22 |
|
|
27 |
|
fputs("Usage: ", stderr); |
28 |
|
fputs(progname, stderr); |
29 |
|
fputs( |
30 |
< |
" [-d ref_depth/unit][-h[io]][-H[io]][-f[afd]] [input [output]]\n", |
30 |
> |
" [-d ref_depth/unit][-h[io]][-H[io]][-f[afd]][-x xr -y yr] [input [output.dpt]]\n", |
31 |
|
stderr); |
32 |
|
fputs(" Or: ", stderr); |
33 |
|
fputs(progname, stderr); |
34 |
|
fputs( |
35 |
< |
" {-r|-p} [-i][-u][-h[io]][-H[io]][-f[afd]] [input [output]]\n", |
35 |
> |
" {-r|-p} [-i][-u][-h[io]][-H[io]][-f[afd]] [input.dpt [output]]\n", |
36 |
|
stderr); |
37 |
|
exit(code); |
38 |
|
} |
42 |
|
static int |
43 |
|
encode_depths(DEPTHCODEC *dcp) |
44 |
|
{ |
45 |
< |
long nexpected = 0; |
45 |
> |
long nexpected = (long)dcp->res.xr * dcp->res.yr; |
46 |
|
|
47 |
|
if (dcp->inpfmt[0]) { |
48 |
< |
if (strcasestr(dcp->inpfmt, "ascii") != NULL) |
48 |
> |
if (!strcmp(dcp->inpfmt, "ascii")) |
49 |
|
dcp->format = 'a'; |
50 |
< |
else if (strcasestr(dcp->inpfmt, "float") != NULL) |
50 |
> |
else if (!strcmp(dcp->inpfmt, "float")) |
51 |
|
dcp->format = 'f'; |
52 |
< |
else if (strcasestr(dcp->inpfmt, "double") != NULL) |
52 |
> |
else if (!strcmp(dcp->inpfmt, "double")) |
53 |
|
dcp->format = 'd'; |
54 |
|
else { |
55 |
|
fputs(dcp->inpname, stderr); |
59 |
|
return 0; |
60 |
|
} |
61 |
|
} |
62 |
< |
if (dcp->hdrflags & HF_RESIN) |
63 |
< |
nexpected = (long)dcp->res.xr * dcp->res.yr; |
62 |
> |
if (dcp->format == 'a') |
63 |
> |
SET_FILE_TEXT(dcp->finp); |
64 |
> |
|
65 |
|
do { |
66 |
|
int ok = 0; |
67 |
|
float f; |
73 |
|
break; |
74 |
|
case 'f': |
75 |
|
ok = (getbinary(&f, sizeof(f), 1, dcp->finp) == 1); |
76 |
+ |
if (dcp->swapped) |
77 |
+ |
swap32((char *)&f, 1); |
78 |
|
d = f; |
79 |
|
break; |
80 |
|
case 'd': |
81 |
|
ok = (getbinary(&d, sizeof(d), 1, dcp->finp) == 1); |
82 |
+ |
if (dcp->swapped) |
83 |
+ |
swap64((char *)&d, 1); |
84 |
|
break; |
85 |
|
} |
86 |
|
if (!ok) |
87 |
|
break; |
88 |
+ |
|
89 |
|
putint(depth2code(d, dcp->refdepth), 2, stdout); |
90 |
+ |
|
91 |
|
} while (--nexpected); |
92 |
|
|
93 |
|
if (nexpected > 0) { |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
< |
/* Convert and output the given depth code to stdout */ |
102 |
> |
/* Convert and output the given depth to stdout */ |
103 |
|
static void |
104 |
|
output_depth(DEPTHCODEC *dcp, double d) |
105 |
|
{ |
124 |
|
static int |
125 |
|
decode_depths(DEPTHCODEC *dcp) |
126 |
|
{ |
127 |
< |
long nexpected = 0; |
127 |
> |
long nexpected = (long)dcp->res.xr * dcp->res.yr; |
128 |
|
|
129 |
|
if (!check_decode_depths(dcp)) |
130 |
|
return 0; |
131 |
|
|
123 |
– |
if (dcp->hdrflags & HF_RESIN) |
124 |
– |
nexpected = (long)dcp->res.xr * dcp->res.yr; |
132 |
|
do { |
133 |
< |
double d = decode_depth_next(dcp); |
133 |
> |
double d = decode_depth_next(dcp); |
134 |
|
if (d < -FTINY) |
135 |
|
break; |
136 |
|
output_depth(dcp, d); |
162 |
|
return 0; |
163 |
|
|
164 |
|
while (scanf("%d %d", &xy[0], &xy[1]) == 2) { |
165 |
< |
loc2pix(xy, &dcp->res, |
166 |
< |
(xy[0]+.5)/dcp->res.xr, (xy[1]+.5)/dcp->res.yr); |
165 |
> |
loc2pix(xy, &dcp->res, xy[0]/(double)dcp->res.xr, |
166 |
> |
xy[1]/(double)dcp->res.yr); |
167 |
|
d = decode_depth_pix(dcp, xy[0], xy[1]); |
168 |
|
if (d < -FTINY) |
169 |
|
return 0; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
< |
/* Output the given world position */ |
186 |
> |
/* Output the given world position to stdout */ |
187 |
|
static void |
188 |
|
output_worldpos(DEPTHCODEC *dcp, FVECT wpos) |
189 |
|
{ |
190 |
|
switch (dcp->format) { |
191 |
|
case 'a': |
192 |
< |
fprintf(stdout, "%.5e %.5e %.5e\n", |
186 |
< |
wpos[0], wpos[1], wpos[2]); |
192 |
> |
printf("%.5e %.5e %.5e\n", wpos[0], wpos[1], wpos[2]); |
193 |
|
break; |
194 |
|
#ifdef SMLFLT |
195 |
|
case 'f': |
254 |
|
} |
255 |
|
if (!check_decode_worldpos(dcp)) |
256 |
|
return 0; |
257 |
< |
|
257 |
> |
|
258 |
|
while (scanf("%d %d", &xy[0], &xy[1]) == 2) { |
259 |
< |
loc2pix(xy, &dcp->res, |
260 |
< |
(xy[0]+.5)/dcp->res.xr, (xy[1]+.5)/dcp->res.yr); |
259 |
> |
loc2pix(xy, &dcp->res, xy[0]/(double)dcp->res.xr, |
260 |
> |
xy[1]/(double)dcp->res.yr); |
261 |
|
if (get_worldpos_pix(wpos, dcp, xy[0], xy[1]) < 0) |
262 |
|
return 0; |
263 |
|
output_worldpos(dcp, wpos); |
279 |
|
int |
280 |
|
main(int argc, char *argv[]) |
281 |
|
{ |
282 |
+ |
int xres=0, yres=0; |
283 |
|
int conversion = CV_FWD; |
284 |
|
int bypixel = 0; |
285 |
|
int unbuffered = 0; |
353 |
|
usage_exit(1); |
354 |
|
} |
355 |
|
break; |
356 |
+ |
case 'x': |
357 |
+ |
xres = atoi(argv[++a]); |
358 |
+ |
break; |
359 |
+ |
case 'y': |
360 |
+ |
yres = atoi(argv[++a]); |
361 |
+ |
break; |
362 |
|
case 'i': |
363 |
|
bypixel++; |
364 |
|
break; |
370 |
|
} |
371 |
|
dc.hdrflags |= (conversion == CV_FWD) * HF_ENCODE; |
372 |
|
|
373 |
< |
if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { |
373 |
> |
if ((xres > 0) & (yres > 0)) { |
374 |
> |
dc.hdrflags &= ~HF_RESIN; |
375 |
> |
dc.res.rt = PIXSTANDARD; |
376 |
> |
dc.res.xr = xres; |
377 |
> |
dc.res.yr = yres; |
378 |
> |
} else if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { |
379 |
|
fputs(progname, stderr); |
380 |
|
fputs(": unknown resolution for output\n", stderr); |
381 |
|
return 1; |
397 |
|
fputs(": -i option requires input resolution\n", stderr); |
398 |
|
usage_exit(1); |
399 |
|
} |
400 |
< |
dc.hdrflags &= ~(HF_HEADOUT|HF_RESOUT); |
400 |
> |
dc.hdrflags &= ~HF_RESOUT; |
401 |
|
} |
402 |
|
if (a < argc-2) { |
403 |
|
fputs(progname, stderr); |
414 |
|
fputs(": cannot open for writing\n", stderr); |
415 |
|
return 1; |
416 |
|
} |
417 |
< |
SET_FILE_BINARY(dc.finp); |
417 |
> |
SET_FILE_BINARY(dc.finp); /* starting assumption */ |
418 |
|
SET_FILE_BINARY(stdout); |
419 |
|
#ifdef getc_unlocked /* avoid stupid semaphores */ |
420 |
|
flockfile(dc.finp); |
421 |
|
flockfile(stdout); |
422 |
|
#endif |
423 |
|
/* read/copy header */ |
424 |
< |
if (!process_dc_header(&dc, argc, argv)) |
424 |
> |
if (!process_dc_header(&dc, a, argv)) |
425 |
|
return 1; |
426 |
+ |
|
427 |
+ |
if ((conversion != CV_FWD) & (dc.format == 'a')) |
428 |
+ |
SET_FILE_TEXT(stdout); |
429 |
|
/* process data */ |
430 |
|
switch (conversion) { |
431 |
|
case CV_FWD: /* distance -> depth code */ |
432 |
+ |
if (!strcmp(dc.depth_unit, "1")) { |
433 |
+ |
fputs(progname, stderr); |
434 |
+ |
fputs(": warning - using reference depth of 1.0\n", |
435 |
+ |
stderr); |
436 |
+ |
} |
437 |
|
if (!encode_depths(&dc)) |
438 |
|
return 1; |
439 |
|
break; |