--- ray/src/util/rcode_ident.c 2019/07/19 22:25:03 2.1 +++ ray/src/util/rcode_ident.c 2019/07/23 17:23:25 2.4 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcode_ident.c,v 2.1 2019/07/19 22:25:03 greg Exp $"; +static const char RCSid[] = "$Id: rcode_ident.c,v 2.4 2019/07/23 17:23:25 greg Exp $"; #endif /* * Create or read identifier index map @@ -17,9 +17,12 @@ static const char RCSid[] = "$Id: rcode_ident.c,v 2.1 #define MAXIDLEN 256 /* longest ID length supported */ #endif +#define HF_TABLOUT 0x100 /* output ID table after header */ + char *progname; /* global argv[0] */ static int unbuffered = 0; +static int numeric = 0; static int sepc = '\n'; @@ -33,7 +36,8 @@ usage_exit(int code) stderr); fputs(" Or: ", stderr); fputs(progname, stderr); - fputs(" -r [-i][-u][-h][-H][-tS] input.idx [output.txt]\n", stderr); + fputs(" -r [-i][-u][-h][-H][-l][-n][-tS] input.idx [output.txt]\n", + stderr); exit(code); } @@ -92,7 +96,9 @@ create_index(const char *fname, int hdrflags, int ndxb long n; int ndx; /* open file if not stdin */ - if (fname && !(fp = fopen(fname, "r"))) { + if (!fname) + fname = ""; + else if (!(fp = fopen(fname, "r"))) { fputs(fname, stderr); fputs(": cannot open for input\n", stderr); return 0; @@ -188,7 +194,8 @@ create_index(const char *fname, int hdrflags, int ndxb stderr); break; } - fclose(fp); /* done with input */ + if (fp != stdin) /* done with input */ + fclose(fp); for (ndx = 0; ndx < nextID; ndx++) { /* append string table */ fputs(idmap[ndx], stdout); putchar('\0'); /* nul-terminated IDs */ @@ -203,6 +210,21 @@ memerr: } +/* print out table IDs */ +void +print_IDs(IDMAP *idmp) +{ + int i; + + printf("============ %d IDs ============\n", idmp->nids); + + for (i = 0; i < idmp->nids; i++) + puts(mapID(idmp, i)); + + puts("============= END ============="); +} + + /* Load selected pixels from identifier index file */ int decode_select(const char *fname, int hdrflags) @@ -213,6 +235,9 @@ decode_select(const char *fname, int hdrflags) if (!idmp) return 0; + if (hdrflags & HF_TABLOUT) + print_IDs(idmp); + if (idmp->res.rt != PIXSTANDARD) { fputs(progname, stderr); fputs(": can only handle standard pixel ordering\n", stderr); @@ -220,21 +245,32 @@ decode_select(const char *fname, int hdrflags) return 0; } while (scanf("%d %d", &x, &y) == 2) { - const char *id; - - if ((x < 0) | (y < 0) | - (x >= idmp->res.xr) | (y >= idmp->res.yr)) { + x = idmap_seek(idmp, x, idmp->res.yr-1 - y); + if (!x) { fputs(progname, stderr); fputs(": warning - pixel index is off map\n", stderr); continue; } - y = idmp->res.yr-1 - y; - - if (!(id = idmap_pix(idmp, x, y))) { + if (x > 0) + x = idmap_next_i(idmp); + if (x < 0) { + fputs(fname, stderr); + fputs(": read/seek error in decode_select()\n", stderr); idmap_close(idmp); return 0; } - fputs(id, stdout); + if (numeric) { + printf("%d", x); + } else { + const char *id = mapID(idmp, x); + if (!id) { + fputs(fname, stderr); + fputs(": bad ID index in file\n", stderr); + idmap_close(idmp); + return 0; + } + fputs(id, stdout); + } putchar(sepc); if (unbuffered && fflush(stdout) == EOF) { fputs(progname, stderr); @@ -264,15 +300,29 @@ decode_all(const char *fname, int hdrflags) if (!idmp) return 0; + if (hdrflags & HF_TABLOUT) + print_IDs(idmp); + for (n = idmp->res.xr*idmp->res.yr; n-- > 0; ) { - const char *id = idmap_next(idmp); - if (!id) { + int ndx = idmap_next_i(idmp); + if (ndx < 0) { fputs(fname, stderr); fputs(": unexpected EOF\n", stderr); idmap_close(idmp); return 0; } - fputs(id, stdout); + if (numeric) { + printf("%d", ndx); + } else { + const char *id = mapID(idmp, ndx); + if (!id) { + fputs(fname, stderr); + fputs(": bad ID index in file\n", stderr); + idmap_close(idmp); + return 0; + } + fputs(id, stdout); + } putchar(sepc); } idmap_close(idmp); @@ -303,6 +353,12 @@ main(int argc, char *argv[]) case 'u': unbuffered++; break; + case 'n': + numeric++; + break; + case 'l': + hdrflags |= HF_TABLOUT; + break; case 'h': hdrflags &= ~HF_HEADOUT; break; @@ -339,6 +395,8 @@ main(int argc, char *argv[]) default: usage_exit(1); } + if ((xres > 0) & (yres > 0)) /* user specified resolution? */ + hdrflags &= ~HF_RESOUT; if (reverse && a >= argc) { fputs(progname, stderr); fputs(": -r option requires named input file\n", stderr);