--- ray/src/util/rcode_depth.c 2019/07/18 18:51:56 2.1 +++ ray/src/util/rcode_depth.c 2019/11/13 18:20:47 2.8 @@ -1,8 +1,8 @@ #ifndef lint -static const char RCSid[] = "$Id: rcode_depth.c,v 2.1 2019/07/18 18:51:56 greg Exp $"; +static const char RCSid[] = "$Id: rcode_depth.c,v 2.8 2019/11/13 18:20:47 greg Exp $"; #endif /* - * Encode and decode depth values using 16-bit integers + * Encode and decode depth map using 16-bit integers */ #include "copyright.h" @@ -15,6 +15,8 @@ static const char RCSid[] = "$Id: rcode_depth.c,v 2.1 #include "fvect.h" #include "depthcodec.h" +char *progname; /* set in main() */ + enum {CV_FWD, CV_REV, CV_PTS}; @@ -25,12 +27,12 @@ usage_exit(int code) fputs("Usage: ", stderr); fputs(progname, stderr); fputs( - " [-d ref_depth/unit][-h[io]][-H[io]][-f[afd]] [input [output]]\n", + " [-d ref_depth/unit][-h[io]][-H[io]][-f[afd]][-x xr -y yr] [input [output.dpt]]\n", stderr); fputs(" Or: ", stderr); fputs(progname, stderr); fputs( - " {-r|-p} [-i][-u][-h[io]][-H[io]][-f[afd]] [input [output]]\n", + " {-r|-p} [-i][-u][-h[io]][-H[io]][-f[afd]] [input.dpt [output]]\n", stderr); exit(code); } @@ -40,14 +42,14 @@ usage_exit(int code) static int encode_depths(DEPTHCODEC *dcp) { - long nexpected = 0; + long nexpected = (long)dcp->res.xr * dcp->res.yr; if (dcp->inpfmt[0]) { - if (strcasestr(dcp->inpfmt, "ascii") != NULL) + if (strstr(dcp->inpfmt, "ascii") != NULL) dcp->format = 'a'; - else if (strcasestr(dcp->inpfmt, "float") != NULL) + else if (strstr(dcp->inpfmt, "float") != NULL) dcp->format = 'f'; - else if (strcasestr(dcp->inpfmt, "double") != NULL) + else if (strstr(dcp->inpfmt, "double") != NULL) dcp->format = 'd'; else { fputs(dcp->inpname, stderr); @@ -57,8 +59,7 @@ encode_depths(DEPTHCODEC *dcp) return 0; } } - if (dcp->hdrflags & HF_RESIN) - nexpected = (long)dcp->res.xr * dcp->res.yr; + do { int ok = 0; float f; @@ -70,15 +71,21 @@ encode_depths(DEPTHCODEC *dcp) break; case 'f': ok = (getbinary(&f, sizeof(f), 1, dcp->finp) == 1); + if (dcp->swapped) + swap32((char *)&f, 1); d = f; break; case 'd': ok = (getbinary(&d, sizeof(d), 1, dcp->finp) == 1); + if (dcp->swapped) + swap64((char *)&d, 1); break; } if (!ok) break; + putint(depth2code(d, dcp->refdepth), 2, stdout); + } while (--nexpected); if (nexpected > 0) { @@ -90,7 +97,7 @@ encode_depths(DEPTHCODEC *dcp) } -/* Convert and output the given depth code to stdout */ +/* Convert and output the given depth to stdout */ static void output_depth(DEPTHCODEC *dcp, double d) { @@ -115,15 +122,13 @@ output_depth(DEPTHCODEC *dcp, double d) static int decode_depths(DEPTHCODEC *dcp) { - long nexpected = 0; + long nexpected = (long)dcp->res.xr * dcp->res.yr; if (!check_decode_depths(dcp)) return 0; - if (dcp->hdrflags & HF_RESIN) - nexpected = (long)dcp->res.xr * dcp->res.yr; do { - double d = decode_depth_next(dcp); + double d = decode_depth_next(dcp); if (d < -FTINY) break; output_depth(dcp, d); @@ -155,12 +160,16 @@ pixel_depths(DEPTHCODEC *dcp, int unbuf) return 0; while (scanf("%d %d", &xy[0], &xy[1]) == 2) { + loc2pix(xy, &dcp->res, (xy[0]+.5)/dcp->res.xr, (xy[1]+.5)/dcp->res.yr); + d = decode_depth_pix(dcp, xy[0], xy[1]); if (d < -FTINY) return 0; + output_depth(dcp, d); + if (unbuf && fflush(stdout) == EOF) { fputs(progname, stderr); fputs(": write error on output\n", stderr); @@ -176,14 +185,13 @@ pixel_depths(DEPTHCODEC *dcp, int unbuf) } -/* Output the given world position */ +/* Output the given world position to stdout */ static void output_worldpos(DEPTHCODEC *dcp, FVECT wpos) { switch (dcp->format) { case 'a': - fprintf(stdout, "%.5e %.5e %.5e\n", - wpos[0], wpos[1], wpos[2]); + printf("%.5e %.5e %.5e\n", wpos[0], wpos[1], wpos[2]); break; #ifdef SMLFLT case 'f': @@ -273,6 +281,7 @@ pixel_points(DEPTHCODEC *dcp, int unbuf) int main(int argc, char *argv[]) { + int xres=0, yres=0; int conversion = CV_FWD; int bypixel = 0; int unbuffered = 0; @@ -346,6 +355,12 @@ main(int argc, char *argv[]) usage_exit(1); } break; + case 'x': + xres = atoi(argv[++a]); + break; + case 'y': + yres = atoi(argv[++a]); + break; case 'i': bypixel++; break; @@ -357,7 +372,12 @@ main(int argc, char *argv[]) } dc.hdrflags |= (conversion == CV_FWD) * HF_ENCODE; - if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { + if ((xres > 0) & (yres > 0)) { + dc.hdrflags &= ~HF_RESIN; + dc.res.rt = PIXSTANDARD; + dc.res.xr = xres; + dc.res.yr = yres; + } else if ((dc.hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESOUT) { fputs(progname, stderr); fputs(": unknown resolution for output\n", stderr); return 1; @@ -379,7 +399,7 @@ main(int argc, char *argv[]) fputs(": -i option requires input resolution\n", stderr); usage_exit(1); } - dc.hdrflags &= ~(HF_HEADOUT|HF_RESOUT); + dc.hdrflags &= ~HF_RESOUT; } if (a < argc-2) { fputs(progname, stderr); @@ -397,13 +417,14 @@ main(int argc, char *argv[]) return 1; } SET_FILE_BINARY(dc.finp); - SET_FILE_BINARY(stdout); + if ((conversion != CV_FWD) | (dc.format != 'a')) + SET_FILE_BINARY(stdout); #ifdef getc_unlocked /* avoid stupid semaphores */ flockfile(dc.finp); flockfile(stdout); #endif /* read/copy header */ - if (!process_dc_header(&dc, argc, argv)) + if (!process_dc_header(&dc, a, argv)) return 1; /* process data */ switch (conversion) {