6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdlib.h> |
9 |
– |
#include <string.h> |
9 |
|
#include <ctype.h> |
10 |
|
#include "platform.h" |
11 |
|
#include "rtio.h" |
19 |
|
#include <sys/mman.h> |
20 |
|
#endif |
21 |
|
|
22 |
+ |
#define MAXLEVELS 16 /* max RxC.. block pairs */ |
23 |
+ |
|
24 |
|
typedef struct { |
25 |
+ |
void *mapped; /* memory-mapped pointer */ |
26 |
|
void *base; /* pointer to base memory */ |
27 |
|
size_t len; /* allocated memory length */ |
26 |
– |
int mapped; /* memory-mapped file? */ |
28 |
|
} MEMLOAD; /* file loaded/mapped into memory */ |
29 |
|
|
30 |
|
typedef struct { |
31 |
|
int nw_rec; /* number of words per record */ |
32 |
< |
int nrecs; /* number of records we found */ |
32 |
> |
long nrecs; /* number of records we found */ |
33 |
|
char *rec[1]; /* record array (extends struct) */ |
34 |
|
} RECINDEX; |
35 |
|
|
43 |
|
return; |
44 |
|
#ifdef MAP_FILE |
45 |
|
if (mp->mapped) |
46 |
< |
munmap(mp->base, mp->len); |
46 |
> |
munmap(mp->mapped, mp->len); |
47 |
|
else |
48 |
|
#endif |
49 |
|
free(mp->base); |
50 |
+ |
mp->mapped = NULL; |
51 |
|
mp->base = NULL; |
52 |
|
mp->len = 0; |
53 |
|
} |
62 |
|
|
63 |
|
if (mp == NULL) |
64 |
|
return(-1); |
65 |
+ |
mp->mapped = NULL; |
66 |
|
mp->base = NULL; |
67 |
|
mp->len = 0; |
65 |
– |
mp->mapped = 0; |
68 |
|
if (fp == NULL) |
69 |
|
return(-1); |
70 |
|
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
100 |
|
#endif |
101 |
|
if (mp == NULL) |
102 |
|
return(-1); |
103 |
+ |
mp->mapped = NULL; |
104 |
|
mp->base = NULL; |
105 |
|
mp->len = 0; |
103 |
– |
mp->mapped = 0; |
106 |
|
if (fp == NULL) |
107 |
|
return(-1); |
108 |
|
fd = fileno(fp); |
113 |
|
mp->len = (size_t)(flen - skip); |
114 |
|
#ifdef MAP_FILE |
115 |
|
if (mp->len > 1L<<20) { /* map file if > 1 MByte */ |
116 |
< |
mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); |
117 |
< |
if (mp->base != MAP_FAILED) { |
118 |
< |
mp->base = (char *)mp->base + skip; |
117 |
< |
mp->mapped = 1; |
116 |
> |
mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); |
117 |
> |
if (mp->mapped != MAP_FAILED) { |
118 |
> |
mp->base = (char *)mp->mapped + skip; |
119 |
|
return(1); /* mmap() success */ |
120 |
|
} |
121 |
< |
mp->base = NULL; /* else fall back to reading it in... */ |
121 |
> |
mp->mapped = NULL; /* else fall back to reading it in... */ |
122 |
|
} |
123 |
|
#endif |
124 |
|
if (lseek(fd, skip, SEEK_SET) != skip || |
218 |
|
|
219 |
|
/* copy nth record from index to stdout */ |
220 |
|
static int |
221 |
< |
print_record(const RECINDEX *rp, int n) |
221 |
> |
print_record(const RECINDEX *rp, long n) |
222 |
|
{ |
223 |
|
int words2go = rp->nw_rec; |
224 |
|
char *scp; |
291 |
|
int transpose = 0; /* transpose rows & cols? */ |
292 |
|
int i_header = 1; /* input header? */ |
293 |
|
int o_header = 1; /* output header? */ |
294 |
+ |
int outArray[MAXLEVELS][2]; /* output block nesting */ |
295 |
+ |
int outLevels = 0; /* number of blocking levels */ |
296 |
|
|
297 |
+ |
/* parse RxCx... string */ |
298 |
+ |
static int |
299 |
+ |
get_array(const char *spec, int blklvl[][2], int nlvls) |
300 |
+ |
{ |
301 |
+ |
int n; |
302 |
+ |
|
303 |
+ |
if (nlvls <= 0) { |
304 |
+ |
fputs("Too many block levels!\n", stderr); |
305 |
+ |
exit(1); |
306 |
+ |
} |
307 |
+ |
if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) { |
308 |
+ |
fputs("Bad block specification!\n", stderr); |
309 |
+ |
exit(1); |
310 |
+ |
} |
311 |
+ |
while (isdigit(*spec)) |
312 |
+ |
spec++; |
313 |
+ |
spec++; /* 'x' */ |
314 |
+ |
while (isdigit(*spec)) |
315 |
+ |
spec++; |
316 |
+ |
if ((*spec != 'x') & (*spec != 'X')) { |
317 |
+ |
if (*spec) { |
318 |
+ |
fputs("Blocks must be separated by 'x' or 'X'\n", stderr); |
319 |
+ |
exit(1); |
320 |
+ |
} |
321 |
+ |
return(1); |
322 |
+ |
} |
323 |
+ |
spec++; |
324 |
+ |
n = get_array(spec, blklvl+1, nlvls-1); |
325 |
+ |
if (!n) |
326 |
+ |
return(0); |
327 |
+ |
blklvl[0][0] *= blklvl[1][0]; |
328 |
+ |
blklvl[0][1] *= blklvl[1][1]; |
329 |
+ |
return(n+1); |
330 |
+ |
} |
331 |
+ |
|
332 |
|
/* check settings and assign defaults */ |
333 |
|
static int |
334 |
|
check_sizes() |
358 |
|
return(1); |
359 |
|
} |
360 |
|
|
361 |
< |
/* output transposed ASCII or binary data from memory */ |
361 |
> |
/* call to compute block input position */ |
362 |
> |
static long |
363 |
> |
get_block_pos(int r, int c, int blklvl[][2], int nlvls) |
364 |
> |
{ |
365 |
> |
long n = 0; |
366 |
> |
|
367 |
> |
while (nlvls > 1) { |
368 |
> |
int sr = r/blklvl[1][0]; |
369 |
> |
int sc = c/blklvl[1][1]; |
370 |
> |
r -= sr*blklvl[1][0]; |
371 |
> |
c -= sc*blklvl[1][1]; |
372 |
> |
n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1]; |
373 |
> |
blklvl++; |
374 |
> |
nlvls--; |
375 |
> |
} |
376 |
> |
n += r*blklvl[0][1] + c; |
377 |
> |
return(n); |
378 |
> |
} |
379 |
> |
|
380 |
> |
/* return input offset based on array ordering and transpose option */ |
381 |
> |
static long |
382 |
> |
get_input_pos(int r, int c) |
383 |
> |
{ |
384 |
> |
long n; |
385 |
> |
|
386 |
> |
if (outLevels > 1) { /* block reordering */ |
387 |
> |
n = get_block_pos(r, c, outArray, outLevels); |
388 |
> |
if (transpose) { |
389 |
> |
r = n/ni_rows; |
390 |
> |
c = n - r*ni_rows; |
391 |
> |
n = (long)c*ni_columns + r; |
392 |
> |
} |
393 |
> |
} else if (transpose) /* transpose only */ |
394 |
> |
n = (long)c*ni_columns + r; |
395 |
> |
else /* XXX should never happen! */ |
396 |
> |
n = (long)r*no_columns + c; |
397 |
> |
return(n); |
398 |
> |
} |
399 |
> |
|
400 |
> |
/* output reordered ASCII or binary data from memory */ |
401 |
|
static int |
402 |
< |
do_transpose(const MEMLOAD *mp) |
402 |
> |
do_reorder(const MEMLOAD *mp) |
403 |
|
{ |
404 |
|
static const char tabEOL[2] = {'\t','\n'}; |
405 |
|
RECINDEX *rp = NULL; |
420 |
|
} else if ((ni_rows > 0) & (ni_columns > 0)) { |
421 |
|
nrecords = ni_rows*ni_columns; |
422 |
|
if (nrecords > mp->len/(n_comp*comp_size)) { |
423 |
< |
fprintf(stderr, |
424 |
< |
"Input too small for specified size and type\n"); |
423 |
> |
fputs("Input too small for specified size and type\n", |
424 |
> |
stderr); |
425 |
|
return(0); |
426 |
|
} |
427 |
|
} else |
433 |
|
ni_columns = nrecords/ni_rows; |
434 |
|
if (nrecords != ni_rows*ni_columns) |
435 |
|
goto badspec; |
436 |
< |
if (no_columns <= 0) |
437 |
< |
no_columns = ni_rows; |
438 |
< |
if (no_rows <= 0) |
439 |
< |
no_rows = ni_columns; |
440 |
< |
if ((no_rows != ni_columns) | (no_columns != ni_rows)) |
441 |
< |
goto badspec; |
442 |
< |
/* transpose records */ |
436 |
> |
if (transpose) { |
437 |
> |
if (no_columns <= 0) |
438 |
> |
no_columns = ni_rows; |
439 |
> |
if (no_rows <= 0) |
440 |
> |
no_rows = ni_columns; |
441 |
> |
if (outLevels <= 1 && |
442 |
> |
(no_rows != ni_columns) | (no_columns != ni_rows)) |
443 |
> |
goto badspec; |
444 |
> |
} else { |
445 |
> |
if (no_columns <= 0) |
446 |
> |
no_columns = ni_columns; |
447 |
> |
if (no_rows <= 0) |
448 |
> |
no_rows = ni_rows; |
449 |
> |
} |
450 |
> |
if (ni_rows*ni_columns != no_rows*no_columns) { |
451 |
> |
fputs("Number of input and output records do not match\n", |
452 |
> |
stderr); |
453 |
> |
return(0); |
454 |
> |
} |
455 |
> |
/* reorder records */ |
456 |
|
for (i = 0; i < no_rows; i++) { |
457 |
< |
for (j = 0; j < no_columns; j++) |
457 |
> |
for (j = 0; j < no_columns; j++) { |
458 |
> |
long n = get_input_pos(i, j); |
459 |
> |
if (n >= nrecords) { |
460 |
> |
fputs("Index past end-of-file\n", stderr); |
461 |
> |
return(0); |
462 |
> |
} |
463 |
|
if (rp != NULL) { /* ASCII output */ |
464 |
< |
print_record(rp, j*ni_columns + i); |
464 |
> |
print_record(rp, n); |
465 |
|
putc(tabEOL[j >= no_columns-1], stdout); |
466 |
|
} else { /* binary output */ |
467 |
< |
putbinary((char *)mp->base + |
373 |
< |
(size_t)(n_comp*comp_size)*(j*ni_columns + i), |
467 |
> |
putbinary((char *)mp->base + (n_comp*comp_size)*n, |
468 |
|
comp_size, n_comp, stdout); |
469 |
|
} |
470 |
+ |
} |
471 |
|
if (ferror(stdout)) { |
472 |
|
fprintf(stderr, "Error writing to stdout\n"); |
473 |
|
return(0); |
477 |
|
free_records(rp); |
478 |
|
return(1); |
479 |
|
badspec: |
480 |
< |
fprintf(stderr, "Bad transpose specification -- check dimension(s)\n"); |
480 |
> |
fprintf(stderr, "Bad dimension(s)\n"); |
481 |
|
return(0); |
482 |
|
} |
483 |
|
|
609 |
|
no_columns = atoi(argv[++a]); |
610 |
|
else if (argv[a][2] == 'r') |
611 |
|
no_rows = atoi(argv[++a]); |
612 |
< |
else |
612 |
> |
else if (argv[a][2] || |
613 |
> |
!(outLevels=get_array(argv[++a], outArray, MAXLEVELS))) |
614 |
|
goto userr; |
615 |
|
break; |
616 |
|
case 'h': /* turn off header */ |
671 |
|
} |
672 |
|
if (a < argc-1) /* arg count OK? */ |
673 |
|
goto userr; |
674 |
+ |
if (outLevels) { /* should check consistency? */ |
675 |
+ |
no_rows = outArray[0][0]; |
676 |
+ |
no_columns = outArray[0][1]; |
677 |
+ |
} |
678 |
|
/* open input file? */ |
679 |
|
if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { |
680 |
|
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
685 |
|
SET_FILE_BINARY(stdout); |
686 |
|
} |
687 |
|
/* check for no-op */ |
688 |
< |
if (!transpose & (i_header == o_header) && |
688 |
> |
if (!transpose & (outLevels <= 1) & (i_header == o_header) && |
689 |
|
(no_columns == ni_columns) & (no_rows == ni_rows)) { |
690 |
|
if (warnings) |
691 |
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
705 |
|
} |
706 |
|
} else if (!check_sizes()) |
707 |
|
return(1); |
708 |
< |
if (o_header) { /* write header */ |
708 |
> |
if (o_header) { /* write/add to header */ |
709 |
> |
if (!i_header) |
710 |
> |
newheader("RADIANCE", stdout); |
711 |
|
printargs(a, argv, stdout); |
712 |
|
if (no_rows > 0) |
713 |
|
printf("NROWS=%d\n", no_rows); |
717 |
|
fputformat(fmtid, stdout); |
718 |
|
fputc('\n', stdout); /* finish new header */ |
719 |
|
} |
720 |
< |
if (transpose) { /* transposing rows & columns? */ |
720 |
> |
if (transpose | (outLevels > 1)) { /* moving stuff around? */ |
721 |
|
MEMLOAD myMem; /* need to map into memory */ |
722 |
|
if (a == argc-1) { |
723 |
|
if (load_file(&myMem, stdin) <= 0) { |
730 |
|
argv[0]); |
731 |
|
return(1); |
732 |
|
} |
733 |
< |
if (!do_transpose(&myMem)) |
733 |
> |
if (!do_reorder(&myMem)) |
734 |
|
return(1); |
735 |
|
/* free_load(&myMem); about to exit, so don't bother */ |
736 |
|
} else if (!do_resize(stdin)) /* reshaping input */ |
738 |
|
return(0); |
739 |
|
userr: |
740 |
|
fprintf(stderr, |
741 |
< |
"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", |
741 |
> |
"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", |
742 |
|
argv[0]); |
743 |
|
return(1); |
744 |
|
} |