--- ray/src/util/rcode_depth.c 2019/07/18 19:13:44 2.2 +++ ray/src/util/rcode_depth.c 2020/07/20 15:53:30 2.11 @@ -1,8 +1,8 @@ #ifndef lint -static const char RCSid[] = "$Id: rcode_depth.c,v 2.2 2019/07/18 19:13:44 greg Exp $"; +static const char RCSid[] = "$Id: rcode_depth.c,v 2.11 2020/07/20 15:53:30 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.2 #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 (!strcmp(dcp->inpfmt, "ascii")) dcp->format = 'a'; - else if (strcasestr(dcp->inpfmt, "float") != NULL) + else if (!strcmp(dcp->inpfmt, "float")) dcp->format = 'f'; - else if (strcasestr(dcp->inpfmt, "double") != NULL) + else if (!strcmp(dcp->inpfmt, "double")) dcp->format = 'd'; else { fputs(dcp->inpname, stderr); @@ -57,8 +59,9 @@ encode_depths(DEPTHCODEC *dcp) return 0; } } - if (dcp->hdrflags & HF_RESIN) - nexpected = (long)dcp->res.xr * dcp->res.yr; + if (dcp->format == 'a') + SET_FILE_TEXT(dcp->finp); + do { int ok = 0; float f; @@ -70,15 +73,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 +99,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 +124,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 +162,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 +187,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': @@ -248,7 +258,7 @@ pixel_points(DEPTHCODEC *dcp, int unbuf) } if (!check_decode_worldpos(dcp)) 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); @@ -273,6 +283,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 +357,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 +374,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 +401,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); @@ -396,7 +418,7 @@ main(int argc, char *argv[]) fputs(": cannot open for writing\n", stderr); return 1; } - SET_FILE_BINARY(dc.finp); + SET_FILE_BINARY(dc.finp); /* starting assumption */ SET_FILE_BINARY(stdout); #ifdef getc_unlocked /* avoid stupid semaphores */ flockfile(dc.finp); @@ -405,9 +427,17 @@ main(int argc, char *argv[]) /* read/copy header */ if (!process_dc_header(&dc, a, argv)) return 1; + + if ((conversion != CV_FWD) & (dc.format == 'a')) + SET_FILE_TEXT(stdout); /* process data */ switch (conversion) { case CV_FWD: /* distance -> depth code */ + if (!strcmp(dc.depth_unit, "1")) { + fputs(progname, stderr); + fputs(": warning - using reference depth of 1.0\n", + stderr); + } if (!encode_depths(&dc)) return 1; break;