| 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 (strstr(dcp->inpfmt, "ascii") != NULL) | 
| 49 |  | dcp->format = 'a'; | 
| 50 | < | else if (strcasestr(dcp->inpfmt, "float") != NULL) | 
| 50 | > | else if (strstr(dcp->inpfmt, "float") != NULL) | 
| 51 |  | dcp->format = 'f'; | 
| 52 | < | else if (strcasestr(dcp->inpfmt, "double") != NULL) | 
| 52 | > | else if (strstr(dcp->inpfmt, "double") != NULL) | 
| 53 |  | dcp->format = 'd'; | 
| 54 |  | else { | 
| 55 |  | fputs(dcp->inpname, stderr); | 
| 59 |  | return 0; | 
| 60 |  | } | 
| 61 |  | } | 
| 62 | < | if (dcp->hdrflags & HF_RESIN) | 
| 61 | < | nexpected = (long)dcp->res.xr * dcp->res.yr; | 
| 62 | > |  | 
| 63 |  | do { | 
| 64 |  | int     ok = 0; | 
| 65 |  | float   f; | 
| 71 |  | break; | 
| 72 |  | case 'f': | 
| 73 |  | ok = (getbinary(&f, sizeof(f), 1, dcp->finp) == 1); | 
| 74 | + | if (dcp->swapped) | 
| 75 | + | swap32((char *)&f, 1); | 
| 76 |  | d = f; | 
| 77 |  | break; | 
| 78 |  | case 'd': | 
| 79 |  | ok = (getbinary(&d, sizeof(d), 1, dcp->finp) == 1); | 
| 80 | + | if (dcp->swapped) | 
| 81 | + | swap64((char *)&d, 1); | 
| 82 |  | break; | 
| 83 |  | } | 
| 84 |  | if (!ok) | 
| 85 |  | break; | 
| 86 | + |  | 
| 87 |  | putint(depth2code(d, dcp->refdepth), 2, stdout); | 
| 88 | + |  | 
| 89 |  | } while (--nexpected); | 
| 90 |  |  | 
| 91 |  | if (nexpected > 0) { | 
| 97 |  | } | 
| 98 |  |  | 
| 99 |  |  | 
| 100 | < | /* Convert and output the given depth code to stdout */ | 
| 100 | > | /* Convert and output the given depth to stdout */ | 
| 101 |  | static void | 
| 102 |  | output_depth(DEPTHCODEC *dcp, double d) | 
| 103 |  | { | 
| 122 |  | static int | 
| 123 |  | decode_depths(DEPTHCODEC *dcp) | 
| 124 |  | { | 
| 125 | < | long    nexpected = 0; | 
| 125 | > | long    nexpected = (long)dcp->res.xr * dcp->res.yr; | 
| 126 |  |  | 
| 127 |  | if (!check_decode_depths(dcp)) | 
| 128 |  | return 0; | 
| 129 |  |  | 
| 123 | – | if (dcp->hdrflags & HF_RESIN) | 
| 124 | – | nexpected = (long)dcp->res.xr * dcp->res.yr; | 
| 130 |  | do { | 
| 131 | < | double  d = decode_depth_next(dcp); | 
| 131 | > | double  d = decode_depth_next(dcp); | 
| 132 |  | if (d < -FTINY) | 
| 133 |  | break; | 
| 134 |  | output_depth(dcp, d); | 
| 160 |  | return 0; | 
| 161 |  |  | 
| 162 |  | while (scanf("%d %d", &xy[0], &xy[1]) == 2) { | 
| 163 | + |  | 
| 164 |  | loc2pix(xy, &dcp->res, | 
| 165 |  | (xy[0]+.5)/dcp->res.xr, (xy[1]+.5)/dcp->res.yr); | 
| 166 | + |  | 
| 167 |  | d = decode_depth_pix(dcp, xy[0], xy[1]); | 
| 168 |  | if (d < -FTINY) | 
| 169 |  | return 0; | 
| 170 | + |  | 
| 171 |  | output_depth(dcp, d); | 
| 172 | + |  | 
| 173 |  | if (unbuf && fflush(stdout) == EOF) { | 
| 174 |  | fputs(progname, stderr); | 
| 175 |  | fputs(": write error on output\n", stderr); | 
| 185 |  | } | 
| 186 |  |  | 
| 187 |  |  | 
| 188 | < | /* Output the given world position */ | 
| 188 | > | /* Output the given world position to stdout */ | 
| 189 |  | static void | 
| 190 |  | output_worldpos(DEPTHCODEC *dcp, FVECT wpos) | 
| 191 |  | { | 
| 192 |  | switch (dcp->format) { | 
| 193 |  | case 'a': | 
| 194 | < | fprintf(stdout, "%.5e %.5e %.5e\n", | 
| 186 | < | wpos[0], wpos[1], wpos[2]); | 
| 194 | > | printf("%.5e %.5e %.5e\n", wpos[0], wpos[1], wpos[2]); | 
| 195 |  | break; | 
| 196 |  | #ifdef SMLFLT | 
| 197 |  | case 'f': | 
| 281 |  | int | 
| 282 |  | main(int argc, char *argv[]) | 
| 283 |  | { | 
| 284 | + | int             xres=0, yres=0; | 
| 285 |  | int             conversion = CV_FWD; | 
| 286 |  | int             bypixel = 0; | 
| 287 |  | int             unbuffered = 0; | 
| 355 |  | usage_exit(1); | 
| 356 |  | } | 
| 357 |  | break; | 
| 358 | + | case 'x': | 
| 359 | + | xres = atoi(argv[++a]); | 
| 360 | + | break; | 
| 361 | + | case 'y': | 
| 362 | + | yres = atoi(argv[++a]); | 
| 363 | + | break; | 
| 364 |  | case 'i': | 
| 365 |  | bypixel++; | 
| 366 |  | break; | 
| 372 |  | } | 
| 373 |  | dc.hdrflags |= (conversion == CV_FWD) * HF_ENCODE; | 
| 374 |  |  | 
| 375 | < | if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { | 
| 375 | > | if ((xres > 0) & (yres > 0)) { | 
| 376 | > | dc.hdrflags &= ~HF_RESIN; | 
| 377 | > | dc.res.rt = PIXSTANDARD; | 
| 378 | > | dc.res.xr = xres; | 
| 379 | > | dc.res.yr = yres; | 
| 380 | > | } else if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { | 
| 381 |  | fputs(progname, stderr); | 
| 382 |  | fputs(": unknown resolution for output\n", stderr); | 
| 383 |  | return 1; | 
| 399 |  | fputs(": -i option requires input resolution\n", stderr); | 
| 400 |  | usage_exit(1); | 
| 401 |  | } | 
| 402 | < | dc.hdrflags &= ~(HF_HEADOUT|HF_RESOUT); | 
| 402 | > | dc.hdrflags &= ~HF_RESOUT; | 
| 403 |  | } | 
| 404 |  | if (a < argc-2) { | 
| 405 |  | fputs(progname, stderr); | 
| 417 |  | return 1; | 
| 418 |  | } | 
| 419 |  | SET_FILE_BINARY(dc.finp); | 
| 420 | < | SET_FILE_BINARY(stdout); | 
| 420 | > | if ((conversion != CV_FWD) | (dc.format != 'a')) | 
| 421 | > | SET_FILE_BINARY(stdout); | 
| 422 |  | #ifdef getc_unlocked                    /* avoid stupid semaphores */ | 
| 423 |  | flockfile(dc.finp); | 
| 424 |  | flockfile(stdout); |