1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.4 |
static const char RCSid[] = "$Id: rcode_ident.c,v 2.3 2019/07/23 17:20:20 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Create or read identifier index map |
6 |
|
|
* |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
#include <stdlib.h> |
10 |
|
|
#include <ctype.h> |
11 |
|
|
#include "rtio.h" |
12 |
|
|
#include "platform.h" |
13 |
|
|
#include "lookup.h" |
14 |
|
|
#include "idmap.h" |
15 |
|
|
|
16 |
|
|
#ifndef MAXIDLEN |
17 |
|
|
#define MAXIDLEN 256 /* longest ID length supported */ |
18 |
|
|
#endif |
19 |
|
|
|
20 |
greg |
2.3 |
#define HF_TABLOUT 0x100 /* output ID table after header */ |
21 |
|
|
|
22 |
greg |
2.1 |
char *progname; /* global argv[0] */ |
23 |
|
|
|
24 |
|
|
static int unbuffered = 0; |
25 |
greg |
2.3 |
static int numeric = 0; |
26 |
greg |
2.1 |
static int sepc = '\n'; |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
/* Report usage error and exit */ |
30 |
|
|
static void |
31 |
|
|
usage_exit(int code) |
32 |
|
|
{ |
33 |
|
|
fputs("Usage: ", stderr); |
34 |
|
|
fputs(progname, stderr); |
35 |
|
|
fputs(" [-8|-16|-24][-h][-tS][-x xr -y yr] [input.txt [output.idx]]\n", |
36 |
|
|
stderr); |
37 |
|
|
fputs(" Or: ", stderr); |
38 |
|
|
fputs(progname, stderr); |
39 |
greg |
2.3 |
fputs(" -r [-i][-u][-h][-H][-l][-n][-tS] input.idx [output.txt]\n", |
40 |
|
|
stderr); |
41 |
greg |
2.1 |
exit(code); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
/* read identifier from input, trimming white space on either end */ |
46 |
|
|
static int |
47 |
|
|
scan_ident(char ident[MAXIDLEN], FILE *fp) |
48 |
|
|
{ |
49 |
|
|
char *cp = ident; |
50 |
|
|
int c; |
51 |
|
|
|
52 |
|
|
while ((c = getc(fp)) != EOF) { |
53 |
|
|
if (cp == ident && isspace(c)) |
54 |
|
|
continue; |
55 |
|
|
if (c == sepc) { |
56 |
|
|
while (cp > ident && isspace(cp[-1])) |
57 |
|
|
cp--; |
58 |
|
|
*cp = '\0'; |
59 |
|
|
return cp - ident; /* return string length */ |
60 |
|
|
} |
61 |
|
|
if (cp - ident >= MAXIDLEN-1) { |
62 |
|
|
fputs(progname, stderr); |
63 |
|
|
fputs(": identifier too long in scan_ident()\n", stderr); |
64 |
|
|
return -1; |
65 |
|
|
} |
66 |
|
|
*cp++ = c; |
67 |
|
|
} |
68 |
|
|
return -1; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/* elide format from header lines */ |
73 |
|
|
static int |
74 |
|
|
headline(char *s, void *p) |
75 |
|
|
{ |
76 |
|
|
extern const char FMTSTR[]; |
77 |
|
|
|
78 |
|
|
if (strstr(s, FMTSTR) == s) |
79 |
|
|
return 0; |
80 |
|
|
|
81 |
|
|
fputs(s, stdout); |
82 |
|
|
return 1; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* Create an identifier index from the given input file (stdin if NULL) */ |
87 |
|
|
int |
88 |
|
|
create_index(const char *fname, int hdrflags, int ndxbytes, int xres, int yres) |
89 |
|
|
{ |
90 |
|
|
FILE *fp = stdin; |
91 |
|
|
char **idmap; |
92 |
|
|
int idmlen; |
93 |
|
|
int nextID = 0; |
94 |
|
|
LUTAB hashtab = LU_SINIT(free,NULL); |
95 |
|
|
RESOLU rs; |
96 |
|
|
long n; |
97 |
|
|
int ndx; |
98 |
|
|
/* open file if not stdin */ |
99 |
greg |
2.2 |
if (!fname) |
100 |
|
|
fname = "<stdin>"; |
101 |
|
|
else if (!(fp = fopen(fname, "r"))) { |
102 |
greg |
2.1 |
fputs(fname, stderr); |
103 |
|
|
fputs(": cannot open for input\n", stderr); |
104 |
|
|
return 0; |
105 |
|
|
} |
106 |
|
|
#ifdef getc_unlocked /* avoid stupid semaphores */ |
107 |
|
|
flockfile(fp); |
108 |
|
|
#endif |
109 |
|
|
/* load/copy header */ |
110 |
|
|
if (hdrflags & HF_HEADOUT && getheader(fp, headline, NULL) < 0) { |
111 |
|
|
fputs(fname, stderr); |
112 |
|
|
fputs(": bad header or wrong input format\n", stderr); |
113 |
|
|
fclose(fp); |
114 |
|
|
return 0; |
115 |
|
|
} |
116 |
|
|
if (!(hdrflags & HF_HEADOUT)) |
117 |
|
|
newheader("RADIANCE", stdout); |
118 |
|
|
/* finish & allocate ID table */ |
119 |
|
|
switch (ndxbytes) { |
120 |
|
|
case 1: |
121 |
|
|
fputformat(IDMAP8FMT, stdout); |
122 |
|
|
idmap = (char **)malloc((idmlen=256)*sizeof(char *)); |
123 |
|
|
break; |
124 |
|
|
case 2: |
125 |
|
|
fputformat(IDMAP16FMT, stdout); |
126 |
|
|
idmap = (char **)malloc((idmlen=4096)*sizeof(char *)); |
127 |
|
|
break; |
128 |
|
|
case 3: |
129 |
|
|
fputformat(IDMAP24FMT, stdout); |
130 |
|
|
idmap = (char **)malloc((idmlen=1L<<17)*sizeof(char *)); |
131 |
|
|
break; |
132 |
|
|
default: |
133 |
|
|
fputs(progname, stderr); |
134 |
|
|
fputs(": unsupported bits/pixel\n", stderr); |
135 |
|
|
return 0; |
136 |
|
|
} |
137 |
|
|
if (!idmap || !lu_init(&hashtab, idmlen)) |
138 |
|
|
goto memerr; |
139 |
|
|
fputc('\n', stdout); /* end of info header */ |
140 |
|
|
|
141 |
|
|
rs.rt = PIXSTANDARD; /* get/write map resolution */ |
142 |
|
|
if (hdrflags & HF_RESOUT) { |
143 |
|
|
if (!fgetsresolu(&rs, fp)) { |
144 |
|
|
fputs(fname, stderr); |
145 |
|
|
fputs(": bad resolution string\n", stderr); |
146 |
|
|
return 0; |
147 |
|
|
} |
148 |
|
|
} else if (((rs.xr = xres) <= 0) | ((rs.yr = yres) <= 0)) { |
149 |
|
|
fputs(fname, stderr); |
150 |
|
|
fputs(": missing/illegal resolution\n", stderr); |
151 |
|
|
return 0; |
152 |
|
|
} |
153 |
|
|
fputsresolu(&rs, stdout); |
154 |
|
|
/* read/assign/write each ID */ |
155 |
|
|
for (n = (long)rs.xr*rs.yr; n-- > 0; ) { |
156 |
|
|
LUENT *lep; |
157 |
|
|
char ident[MAXIDLEN]; |
158 |
|
|
int len = scan_ident(ident, fp); |
159 |
|
|
if (len < 0) { |
160 |
|
|
if (feof(fp)) { |
161 |
|
|
fputs(fname, stderr); |
162 |
|
|
fputs(": unexpected EOF\n", stderr); |
163 |
|
|
} |
164 |
|
|
return 0; |
165 |
|
|
} |
166 |
|
|
lep = lu_find(&hashtab, ident); |
167 |
|
|
if (!lep->key) { /* new entry */ |
168 |
|
|
if (nextID >= 1<<(ndxbytes<<3)) { |
169 |
|
|
fputs(progname, stderr); |
170 |
|
|
fputs( |
171 |
|
|
": too many unique identifiers for index size\n", |
172 |
|
|
stderr); |
173 |
|
|
return 0; |
174 |
|
|
} |
175 |
|
|
if (nextID >= idmlen) { /* reallocate? */ |
176 |
|
|
idmlen += idmlen>>2; /* +25% growth */ |
177 |
|
|
idmap = (char **)realloc(idmap, idmlen); |
178 |
|
|
if (!idmap) |
179 |
|
|
goto memerr; |
180 |
|
|
} |
181 |
|
|
lep->key = (char *)malloc(len+1); |
182 |
|
|
if (!lep->key) |
183 |
|
|
goto memerr; |
184 |
|
|
idmap[nextID] = strcpy(lep->key, ident); |
185 |
|
|
lep->data = (char *)nextID++; |
186 |
|
|
} |
187 |
|
|
/* write this id index */ |
188 |
|
|
putint((int)lep->data, ndxbytes, stdout); |
189 |
|
|
} |
190 |
|
|
while ((ndx = getc(fp)) != EOF) /* verify we got them all */ |
191 |
|
|
if (!isspace(ndx)) { |
192 |
|
|
fputs(fname, stderr); |
193 |
|
|
fputs(": warning - non-white characters past end\n", |
194 |
|
|
stderr); |
195 |
|
|
break; |
196 |
|
|
} |
197 |
greg |
2.2 |
if (fp != stdin) /* done with input */ |
198 |
|
|
fclose(fp); |
199 |
greg |
2.1 |
for (ndx = 0; ndx < nextID; ndx++) { /* append string table */ |
200 |
|
|
fputs(idmap[ndx], stdout); |
201 |
|
|
putchar('\0'); /* nul-terminated IDs */ |
202 |
|
|
} |
203 |
|
|
free(idmap); /* clean up */ |
204 |
|
|
lu_done(&hashtab); |
205 |
|
|
return 1; |
206 |
|
|
memerr: |
207 |
|
|
fputs(progname, stderr); |
208 |
|
|
fputs(": out of memory!\n", stderr); |
209 |
|
|
return 0; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
|
213 |
greg |
2.3 |
/* print out table IDs */ |
214 |
|
|
void |
215 |
|
|
print_IDs(IDMAP *idmp) |
216 |
|
|
{ |
217 |
|
|
int i; |
218 |
|
|
|
219 |
|
|
printf("============ %d IDs ============\n", idmp->nids); |
220 |
|
|
|
221 |
|
|
for (i = 0; i < idmp->nids; i++) |
222 |
|
|
puts(mapID(idmp, i)); |
223 |
|
|
|
224 |
|
|
puts("============= END ============="); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
greg |
2.1 |
/* Load selected pixels from identifier index file */ |
229 |
|
|
int |
230 |
|
|
decode_select(const char *fname, int hdrflags) |
231 |
|
|
{ |
232 |
|
|
IDMAP *idmp = idmap_ropen(fname, hdrflags); |
233 |
|
|
int x, y; |
234 |
|
|
|
235 |
|
|
if (!idmp) |
236 |
|
|
return 0; |
237 |
|
|
|
238 |
greg |
2.3 |
if (hdrflags & HF_TABLOUT) |
239 |
|
|
print_IDs(idmp); |
240 |
|
|
|
241 |
greg |
2.1 |
if (idmp->res.rt != PIXSTANDARD) { |
242 |
|
|
fputs(progname, stderr); |
243 |
|
|
fputs(": can only handle standard pixel ordering\n", stderr); |
244 |
|
|
idmap_close(idmp); |
245 |
|
|
return 0; |
246 |
|
|
} |
247 |
|
|
while (scanf("%d %d", &x, &y) == 2) { |
248 |
greg |
2.3 |
x = idmap_seek(idmp, x, idmp->res.yr-1 - y); |
249 |
|
|
if (!x) { |
250 |
greg |
2.1 |
fputs(progname, stderr); |
251 |
|
|
fputs(": warning - pixel index is off map\n", stderr); |
252 |
|
|
continue; |
253 |
|
|
} |
254 |
greg |
2.3 |
if (x > 0) |
255 |
|
|
x = idmap_next_i(idmp); |
256 |
|
|
if (x < 0) { |
257 |
|
|
fputs(fname, stderr); |
258 |
|
|
fputs(": read/seek error in decode_select()\n", stderr); |
259 |
greg |
2.1 |
idmap_close(idmp); |
260 |
|
|
return 0; |
261 |
|
|
} |
262 |
greg |
2.3 |
if (numeric) { |
263 |
|
|
printf("%d", x); |
264 |
|
|
} else { |
265 |
|
|
const char *id = mapID(idmp, x); |
266 |
|
|
if (!id) { |
267 |
|
|
fputs(fname, stderr); |
268 |
|
|
fputs(": bad ID index in file\n", stderr); |
269 |
|
|
idmap_close(idmp); |
270 |
|
|
return 0; |
271 |
|
|
} |
272 |
|
|
fputs(id, stdout); |
273 |
|
|
} |
274 |
greg |
2.1 |
putchar(sepc); |
275 |
|
|
if (unbuffered && fflush(stdout) == EOF) { |
276 |
|
|
fputs(progname, stderr); |
277 |
|
|
fputs(": write error on output\n", stderr); |
278 |
|
|
idmap_close(idmp); |
279 |
|
|
return 0; |
280 |
|
|
} |
281 |
|
|
} |
282 |
|
|
idmap_close(idmp); |
283 |
|
|
|
284 |
|
|
if (!feof(stdin)) { |
285 |
|
|
fputs(progname, stderr); |
286 |
|
|
fputs(": non-numeric input for pixel index\n", stderr); |
287 |
|
|
return 0; |
288 |
|
|
} |
289 |
|
|
return 1; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
|
293 |
|
|
/* Load and convert identifier index file to ASCII */ |
294 |
|
|
int |
295 |
|
|
decode_all(const char *fname, int hdrflags) |
296 |
|
|
{ |
297 |
|
|
IDMAP *idmp = idmap_ropen(fname, hdrflags); |
298 |
|
|
long n; |
299 |
|
|
|
300 |
|
|
if (!idmp) |
301 |
|
|
return 0; |
302 |
|
|
|
303 |
greg |
2.3 |
if (hdrflags & HF_TABLOUT) |
304 |
|
|
print_IDs(idmp); |
305 |
|
|
|
306 |
greg |
2.1 |
for (n = idmp->res.xr*idmp->res.yr; n-- > 0; ) { |
307 |
greg |
2.3 |
int ndx = idmap_next_i(idmp); |
308 |
|
|
if (ndx < 0) { |
309 |
greg |
2.1 |
fputs(fname, stderr); |
310 |
|
|
fputs(": unexpected EOF\n", stderr); |
311 |
|
|
idmap_close(idmp); |
312 |
|
|
return 0; |
313 |
|
|
} |
314 |
greg |
2.3 |
if (numeric) { |
315 |
|
|
printf("%d", ndx); |
316 |
|
|
} else { |
317 |
greg |
2.4 |
const char *id = mapID(idmp, ndx); |
318 |
greg |
2.3 |
if (!id) { |
319 |
|
|
fputs(fname, stderr); |
320 |
|
|
fputs(": bad ID index in file\n", stderr); |
321 |
|
|
idmap_close(idmp); |
322 |
|
|
return 0; |
323 |
|
|
} |
324 |
|
|
fputs(id, stdout); |
325 |
|
|
} |
326 |
greg |
2.1 |
putchar(sepc); |
327 |
|
|
} |
328 |
|
|
idmap_close(idmp); |
329 |
|
|
return 1; |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
|
333 |
|
|
int |
334 |
|
|
main(int argc, char *argv[]) |
335 |
|
|
{ |
336 |
|
|
char *inpfname = NULL; |
337 |
|
|
int reverse = 0; |
338 |
|
|
int bypixel = 0; |
339 |
|
|
int hdrflags = HF_HEADOUT|HF_RESOUT; |
340 |
|
|
int xres=0, yres=0; |
341 |
|
|
int ndxbytes = 2; |
342 |
|
|
int a; |
343 |
|
|
|
344 |
|
|
progname = argv[0]; |
345 |
|
|
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
346 |
|
|
switch (argv[a][1]) { |
347 |
|
|
case 'r': |
348 |
|
|
reverse++; |
349 |
|
|
break; |
350 |
|
|
case 'i': |
351 |
|
|
bypixel++; |
352 |
|
|
break; |
353 |
|
|
case 'u': |
354 |
|
|
unbuffered++; |
355 |
|
|
break; |
356 |
greg |
2.3 |
case 'n': |
357 |
|
|
numeric++; |
358 |
|
|
break; |
359 |
|
|
case 'l': |
360 |
|
|
hdrflags |= HF_TABLOUT; |
361 |
|
|
break; |
362 |
greg |
2.1 |
case 'h': |
363 |
|
|
hdrflags &= ~HF_HEADOUT; |
364 |
|
|
break; |
365 |
|
|
case 'H': |
366 |
|
|
hdrflags &= ~HF_RESOUT; |
367 |
|
|
break; |
368 |
|
|
case 't': |
369 |
|
|
if (!(sepc = argv[a][2])) |
370 |
|
|
sepc = '\n'; |
371 |
|
|
break; |
372 |
|
|
case 'x': |
373 |
|
|
xres = atoi(argv[++a]); |
374 |
|
|
break; |
375 |
|
|
case 'y': |
376 |
|
|
yres = atoi(argv[++a]); |
377 |
|
|
break; |
378 |
|
|
case '8': |
379 |
|
|
case '1': |
380 |
|
|
case '2': |
381 |
|
|
switch (atoi(argv[a]+1)) { |
382 |
|
|
case 8: |
383 |
|
|
ndxbytes = 1; |
384 |
|
|
break; |
385 |
|
|
case 16: |
386 |
|
|
ndxbytes = 2; |
387 |
|
|
break; |
388 |
|
|
case 24: |
389 |
|
|
ndxbytes = 3; |
390 |
|
|
break; |
391 |
|
|
default: |
392 |
|
|
usage_exit(1); |
393 |
|
|
} |
394 |
|
|
break; |
395 |
|
|
default: |
396 |
|
|
usage_exit(1); |
397 |
|
|
} |
398 |
greg |
2.2 |
if ((xres > 0) & (yres > 0)) /* user specified resolution? */ |
399 |
|
|
hdrflags &= ~HF_RESOUT; |
400 |
greg |
2.1 |
if (reverse && a >= argc) { |
401 |
|
|
fputs(progname, stderr); |
402 |
|
|
fputs(": -r option requires named input file\n", stderr); |
403 |
|
|
return 1; |
404 |
|
|
} |
405 |
|
|
if (a < argc-2) { |
406 |
|
|
fputs(progname, stderr); |
407 |
|
|
fputs(": too many file arguments\n", stderr); |
408 |
|
|
return 1; |
409 |
|
|
} |
410 |
|
|
if (a < argc) |
411 |
|
|
inpfname = argv[a]; |
412 |
|
|
if (a+1 < argc && !freopen(argv[a+1], "w", stdout)) { |
413 |
|
|
fputs(argv[a+1], stderr); |
414 |
|
|
fputs(": cannot open output\n", stderr); |
415 |
|
|
return 1; |
416 |
|
|
} |
417 |
|
|
if (!reverse) |
418 |
|
|
SET_FILE_BINARY(stdout); |
419 |
|
|
#ifdef getc_unlocked /* avoid stupid semaphores */ |
420 |
|
|
flockfile(stdout); |
421 |
|
|
#endif |
422 |
|
|
if (reverse) { |
423 |
|
|
if (!(bypixel ? decode_select(inpfname, hdrflags) : |
424 |
|
|
decode_all(inpfname, hdrflags))) |
425 |
|
|
return 1; |
426 |
|
|
} else if (!create_index(inpfname, hdrflags, ndxbytes, xres, yres)) |
427 |
|
|
return 1; |
428 |
|
|
|
429 |
|
|
if (fflush(stdout) == EOF) { |
430 |
|
|
fputs(progname, stderr); |
431 |
|
|
fputs(": error writing output\n", stderr); |
432 |
|
|
return 1; |
433 |
|
|
} |
434 |
|
|
return 0; |
435 |
|
|
} |