--- ray/src/util/rcode_ident.c 2019/07/19 22:25:03 2.1 +++ ray/src/util/rcode_ident.c 2019/07/24 00:25:51 2.6 @@ -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.6 2019/07/24 00:25:51 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); } @@ -87,12 +91,14 @@ create_index(const char *fname, int hdrflags, int ndxb char **idmap; int idmlen; int nextID = 0; - LUTAB hashtab = LU_SINIT(free,NULL); + LUTAB hashtab; RESOLU rs; 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; @@ -128,6 +134,10 @@ create_index(const char *fname, int hdrflags, int ndxb fputs(": unsupported bits/pixel\n", stderr); return 0; } + memset(&hashtab, 0, sizeof(hashtab)); + hashtab.hashf = lu_shash; + hashtab.keycmp = strcmp; + hashtab.freek = free; if (!idmap || !lu_init(&hashtab, idmlen)) goto memerr; fputc('\n', stdout); /* end of info header */ @@ -188,7 +198,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,38 +214,67 @@ memerr: } +/* print out ID table */ +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) { IDMAP *idmp = idmap_ropen(fname, hdrflags); - int x, y; + int i, j; 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); idmap_close(idmp); 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)) { + while (scanf("%d %d", &i, &j) == 2) { + i = idmap_seek(idmp, i, idmp->res.yr-1 - j); + if (!i) { 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 (i > 0) + i = idmap_next_i(idmp); + if (i < 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", i); + } else { + const char *id = mapID(idmp, i); + 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 +304,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) { + const 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 +357,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 +399,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);