| 11 |  | #include "platform.h" | 
| 12 |  | #include "rtio.h" | 
| 13 |  | #include "resolu.h" | 
| 14 | < | #ifdef _WIN32 | 
| 15 | < | #undef ftello | 
| 16 | < | #define ftello  ftell | 
| 17 | < | #undef ssize_t | 
| 18 | < | #define ssize_t size_t | 
| 14 | > | #if defined(_WIN32) || defined(_WIN64) | 
| 15 | > | #undef ftello | 
| 16 | > | #define       ftello  ftell | 
| 17 | > | #undef ssize_t | 
| 18 | > | #define ssize_t       size_t | 
| 19 |  | #else | 
| 20 | < | #include <sys/mman.h> | 
| 20 | > | #include <sys/mman.h> | 
| 21 |  | #endif | 
| 22 |  |  | 
| 23 | – | #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */ | 
| 24 | – | #undef getc | 
| 25 | – | #undef putc | 
| 26 | – | #define getc    getc_unlocked | 
| 27 | – | #define putc    putc_unlocked | 
| 28 | – | #endif | 
| 29 | – |  | 
| 23 |  | typedef struct { | 
| 24 | + | void    *mapped;        /* memory-mapped pointer */ | 
| 25 |  | void    *base;          /* pointer to base memory */ | 
| 26 |  | size_t  len;            /* allocated memory length */ | 
| 33 | – | int     mapped;         /* memory-mapped file? */ | 
| 27 |  | } MEMLOAD;              /* file loaded/mapped into memory */ | 
| 28 |  |  | 
| 29 |  | typedef struct { | 
| 42 |  | return; | 
| 43 |  | #ifdef MAP_FILE | 
| 44 |  | if (mp->mapped) | 
| 45 | < | munmap(mp->base, mp->len); | 
| 45 | > | munmap(mp->mapped, mp->len); | 
| 46 |  | else | 
| 47 |  | #endif | 
| 48 |  | free(mp->base); | 
| 49 | + | mp->mapped = NULL; | 
| 50 |  | mp->base = NULL; | 
| 51 |  | mp->len = 0; | 
| 52 |  | } | 
| 53 |  |  | 
| 60 | – | /* load a file into memory */ | 
| 61 | – | static int | 
| 62 | – | load_file(MEMLOAD *mp, FILE *fp) | 
| 63 | – | { | 
| 64 | – | int     fd; | 
| 65 | – | off_t   skip, flen; | 
| 66 | – |  | 
| 67 | – | if (mp == NULL) | 
| 68 | – | return(-1); | 
| 69 | – | mp->base = NULL; | 
| 70 | – | mp->len = 0; | 
| 71 | – | mp->mapped = 0; | 
| 72 | – | if (fp == NULL) | 
| 73 | – | return(-1); | 
| 74 | – | fd = fileno(fp); | 
| 75 | – | skip = ftello(fp); | 
| 76 | – | flen = lseek(fd, 0, SEEK_END); | 
| 77 | – | if (flen <= skip) | 
| 78 | – | return((int)(flen - skip)); | 
| 79 | – | mp->len = (size_t)(flen - skip); | 
| 80 | – | #ifdef MAP_FILE | 
| 81 | – | if (mp->len > 1L<<20) {         /* map file if > 1 MByte */ | 
| 82 | – | mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip); | 
| 83 | – | if (mp->base != MAP_FAILED) { | 
| 84 | – | mp->mapped = 1; | 
| 85 | – | return(1);      /* mmap() success */ | 
| 86 | – | } | 
| 87 | – | mp->base = NULL;        /* fall back to reading it in... */ | 
| 88 | – | } | 
| 89 | – | #endif | 
| 90 | – | if (lseek(fd, skip, SEEK_SET) != skip || | 
| 91 | – | (mp->base = malloc(mp->len)) == NULL) { | 
| 92 | – | mp->len = 0; | 
| 93 | – | return(-1); | 
| 94 | – | } | 
| 95 | – | if (read(fd, (char *)mp->base, mp->len) != mp->len) { | 
| 96 | – | free_load(mp); | 
| 97 | – | return(-1); | 
| 98 | – | } | 
| 99 | – | return(1); | 
| 100 | – | } | 
| 101 | – |  | 
| 54 |  | /* load memory from an input stream, starting from current position */ | 
| 55 |  | static int | 
| 56 |  | load_stream(MEMLOAD *mp, FILE *fp) | 
| 61 |  |  | 
| 62 |  | if (mp == NULL) | 
| 63 |  | return(-1); | 
| 64 | + | mp->mapped = NULL; | 
| 65 |  | mp->base = NULL; | 
| 66 |  | mp->len = 0; | 
| 114 | – | mp->mapped = 0; | 
| 67 |  | if (fp == NULL) | 
| 68 |  | return(-1); | 
| 69 |  | while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { | 
| 70 |  | if (!alloced) | 
| 71 | < | mp->base = malloc(nr); | 
| 71 | > | mp->base = malloc(alloced = nr); | 
| 72 |  | else if (mp->len+nr > alloced) | 
| 73 |  | mp->base = realloc(mp->base, | 
| 74 |  | alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); | 
| 86 |  | return(mp->len > 0); | 
| 87 |  | } | 
| 88 |  |  | 
| 89 | + | /* load a file into memory */ | 
| 90 | + | static int | 
| 91 | + | load_file(MEMLOAD *mp, FILE *fp) | 
| 92 | + | { | 
| 93 | + | int     fd; | 
| 94 | + | off_t   skip, flen, fpos; | 
| 95 | + |  | 
| 96 | + | #if defined(_WIN32) || defined(_WIN64) | 
| 97 | + | /* too difficult to fix this */ | 
| 98 | + | return load_stream(mp, fp); | 
| 99 | + | #endif | 
| 100 | + | if (mp == NULL) | 
| 101 | + | return(-1); | 
| 102 | + | mp->mapped = NULL; | 
| 103 | + | mp->base = NULL; | 
| 104 | + | mp->len = 0; | 
| 105 | + | if (fp == NULL) | 
| 106 | + | return(-1); | 
| 107 | + | fd = fileno(fp); | 
| 108 | + | skip = ftello(fp); | 
| 109 | + | flen = lseek(fd, 0, SEEK_END); | 
| 110 | + | if (flen <= skip) | 
| 111 | + | return((int)(flen - skip)); | 
| 112 | + | mp->len = (size_t)(flen - skip); | 
| 113 | + | #ifdef MAP_FILE | 
| 114 | + | if (mp->len > 1L<<20) {         /* map file if > 1 MByte */ | 
| 115 | + | mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); | 
| 116 | + | if (mp->mapped != MAP_FAILED) { | 
| 117 | + | mp->base = (char *)mp->mapped + skip; | 
| 118 | + | return(1);      /* mmap() success */ | 
| 119 | + | } | 
| 120 | + | mp->mapped = NULL;      /* else fall back to reading it in... */ | 
| 121 | + | } | 
| 122 | + | #endif | 
| 123 | + | if (lseek(fd, skip, SEEK_SET) != skip || | 
| 124 | + | (mp->base = malloc(mp->len)) == NULL) { | 
| 125 | + | mp->len = 0; | 
| 126 | + | return(-1); | 
| 127 | + | } | 
| 128 | + | fpos = skip; | 
| 129 | + | while (fpos < flen) {           /* read() fails if n > 2 GBytes */ | 
| 130 | + | ssize_t nread = read(fd, (char *)mp->base+(fpos-skip), | 
| 131 | + | (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24); | 
| 132 | + | if (nread <= 0) { | 
| 133 | + | free_load(mp); | 
| 134 | + | return(-1); | 
| 135 | + | } | 
| 136 | + | fpos += nread; | 
| 137 | + | } | 
| 138 | + | return(1); | 
| 139 | + | } | 
| 140 | + |  | 
| 141 |  | /* free a record index */ | 
| 142 |  | #define free_records(rp)        free(rp) | 
| 143 |  |  | 
| 145 |  | static RECINDEX * | 
| 146 |  | index_records(const MEMLOAD *mp, int nw_rec) | 
| 147 |  | { | 
| 148 | + | int             nall = 0; | 
| 149 |  | RECINDEX        *rp; | 
| 150 |  | char            *cp, *mend; | 
| 151 |  | int             n; | 
| 154 |  | return(NULL); | 
| 155 |  | if (nw_rec <= 0) | 
| 156 |  | return(NULL); | 
| 157 | < | rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *)); | 
| 157 | > | nall = 1000; | 
| 158 | > | rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); | 
| 159 |  | if (rp == NULL) | 
| 160 |  | return(NULL); | 
| 161 |  | rp->nw_rec = nw_rec; | 
| 167 |  | ++cp; | 
| 168 |  | if (cp >= mend) | 
| 169 |  | break; | 
| 170 | + | if (rp->nrecs >= nall) { | 
| 171 | + | nall += nall>>1;        /* get more record space */ | 
| 172 | + | rp = (RECINDEX *)realloc(rp, | 
| 173 | + | sizeof(RECINDEX) + nall*sizeof(char *)); | 
| 174 | + | if (rp == NULL) | 
| 175 | + | return(NULL); | 
| 176 | + | } | 
| 177 |  | rp->rec[rp->nrecs++] = cp;      /* point to first non-white */ | 
| 178 |  | n = rp->nw_rec; | 
| 179 |  | while (++cp < mend)             /* find end of record */ | 
| 250 |  |  | 
| 251 |  | if (fp == NULL) | 
| 252 |  | return(0); | 
| 253 | < | fflush(stdout);                 /* assumes nothing in input buffer */ | 
| 254 | < | while ((n = read(fileno(fp), buf, sizeof(buf))) > 0) | 
| 253 | > | fflush(stdout); | 
| 254 | > | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) | 
| 255 |  | if (write(fileno(stdout), buf, n) != n) | 
| 256 |  | return(0); | 
| 257 | < | return(n >= 0); | 
| 257 | > | return(!ferror(fp)); | 
| 258 |  | } | 
| 259 |  |  | 
| 260 |  | /* get next word from stream, leaving stream on EOL or start of next word */ | 
| 280 |  | return(buf); | 
| 281 |  | } | 
| 282 |  |  | 
| 283 | < | char            *fmtid = "ascii";               /* format id */ | 
| 284 | < | int             record_width = 3;               /* words/record (<0 binary) */ | 
| 283 | > | char            *fmtid = NULL;                  /* format id */ | 
| 284 | > | int             comp_size = 0;                  /* binary bytes/channel */ | 
| 285 | > | int             n_comp = 0;                     /* components/record */ | 
| 286 |  | int             ni_columns = 0;                 /* number of input columns */ | 
| 287 |  | int             ni_rows = 0;                    /* number of input rows */ | 
| 288 |  | int             no_columns = 0;                 /* number of output columns */ | 
| 289 |  | int             no_rows = 0;                    /* number of output rows */ | 
| 290 | + | int             transpose = 0;                  /* transpose rows & cols? */ | 
| 291 | + | int             i_header = 1;                   /* input header? */ | 
| 292 | + | int             o_header = 1;                   /* output header? */ | 
| 293 |  |  | 
| 294 | + | /* check settings and assign defaults */ | 
| 295 | + | static int | 
| 296 | + | check_sizes() | 
| 297 | + | { | 
| 298 | + | if (fmtid == NULL) { | 
| 299 | + | fmtid = "ascii"; | 
| 300 | + | } else if (!comp_size) { | 
| 301 | + | if (!strcmp(fmtid, "float")) | 
| 302 | + | comp_size = sizeof(float); | 
| 303 | + | else if (!strcmp(fmtid, "double")) | 
| 304 | + | comp_size = sizeof(double); | 
| 305 | + | else if (!strcmp(fmtid, "byte")) | 
| 306 | + | comp_size = 1; | 
| 307 | + | else if (strcmp(fmtid, "ascii")) { | 
| 308 | + | fprintf(stderr, "Unsupported format: %s\n", fmtid); | 
| 309 | + | return(0); | 
| 310 | + | } | 
| 311 | + | } | 
| 312 | + | if (transpose && (no_rows <= 0) & (no_columns <= 0)) { | 
| 313 | + | if (ni_rows > 0) no_columns = ni_rows; | 
| 314 | + | if (ni_columns > 0) no_rows = ni_columns; | 
| 315 | + | } else if ((no_rows <= 0) & (no_columns > 0) && | 
| 316 | + | !((ni_rows*ni_columns) % no_columns)) | 
| 317 | + | no_rows = ni_rows*ni_columns/no_columns; | 
| 318 | + | if (n_comp <= 0) | 
| 319 | + | n_comp = 3; | 
| 320 | + | return(1); | 
| 321 | + | } | 
| 322 | + |  | 
| 323 |  | /* output transposed ASCII or binary data from memory */ | 
| 324 |  | static int | 
| 325 |  | do_transpose(const MEMLOAD *mp) | 
| 334 |  | if (ni_columns <= 0) | 
| 335 |  | ni_columns = no_rows; | 
| 336 |  | /* get # records (& index) */ | 
| 337 | < | if (record_width > 0) { | 
| 338 | < | if ((rp = index_records(mp, record_width)) == NULL) | 
| 337 | > | if (!comp_size) { | 
| 338 | > | if ((rp = index_records(mp, n_comp)) == NULL) | 
| 339 |  | return(0); | 
| 340 |  | if (ni_columns <= 0) | 
| 341 |  | ni_columns = count_columns(rp); | 
| 342 |  | nrecords = rp->nrecs; | 
| 343 |  | } else if ((ni_rows > 0) & (ni_columns > 0)) { | 
| 344 |  | nrecords = ni_rows*ni_columns; | 
| 345 | < | if (nrecords > mp->len / -record_width) { | 
| 345 | > | if (nrecords > mp->len/(n_comp*comp_size)) { | 
| 346 |  | fprintf(stderr, | 
| 347 |  | "Input too small for specified size and type\n"); | 
| 348 |  | return(0); | 
| 349 |  | } | 
| 350 |  | } else | 
| 351 | < | nrecords = mp->len / -record_width; | 
| 351 | > | nrecords = mp->len/(n_comp*comp_size); | 
| 352 |  | /* check sizes */ | 
| 353 |  | if ((ni_rows <= 0) & (ni_columns > 0)) | 
| 354 |  | ni_rows = nrecords/ni_columns; | 
| 369 |  | print_record(rp, j*ni_columns + i); | 
| 370 |  | putc(tabEOL[j >= no_columns-1], stdout); | 
| 371 |  | } else {                        /* binary output */ | 
| 372 | < | fwrite((char *)mp->base + | 
| 373 | < | -record_width*(j*ni_columns + i), | 
| 374 | < | -record_width, 1, stdout); | 
| 372 | > | putbinary((char *)mp->base + | 
| 373 | > | (size_t)(n_comp*comp_size)*(j*ni_columns + i), | 
| 374 | > | comp_size, n_comp, stdout); | 
| 375 |  | } | 
| 376 |  | if (ferror(stdout)) { | 
| 377 |  | fprintf(stderr, "Error writing to stdout\n"); | 
| 394 |  | int     columns2go = no_columns; | 
| 395 |  | char    word[256]; | 
| 396 |  | /* sanity checks */ | 
| 397 | < | if (record_width <= 0) { | 
| 398 | < | fprintf(stderr, "Bad call to do_resize (record_width = %d)\n", | 
| 353 | < | record_width); | 
| 354 | < | return(0); | 
| 355 | < | } | 
| 397 | > | if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows)) | 
| 398 | > | return(output_stream(fp));      /* no-op -- just copy */ | 
| 399 |  | if (no_columns <= 0) { | 
| 400 |  | fprintf(stderr, "Missing -oc specification\n"); | 
| 401 |  | return(0); | 
| 411 |  | do {                                    /* reshape records */ | 
| 412 |  | int     n; | 
| 413 |  |  | 
| 414 | < | for (n = record_width; n--; ) { | 
| 414 | > | for (n = n_comp; n--; ) { | 
| 415 |  | if (fget_word(word, fp) == NULL) { | 
| 416 | < | if (records2go > 0 || n < record_width-1) | 
| 416 | > | if (records2go > 0 || n < n_comp-1) | 
| 417 |  | break; | 
| 418 |  | goto done;      /* normal EOD */ | 
| 419 |  | } | 
| 448 |  | static int | 
| 449 |  | headline(char *s, void *p) | 
| 450 |  | { | 
| 451 | < | char    fmt[32]; | 
| 451 | > | static char     fmt[MAXFMTLEN]; | 
| 452 | > | int             n; | 
| 453 |  |  | 
| 454 |  | if (formatval(fmt, s)) { | 
| 455 | + | if (fmtid == NULL) { | 
| 456 | + | fmtid = fmt; | 
| 457 | + | return(0); | 
| 458 | + | } | 
| 459 |  | if (!strcmp(fmt, fmtid)) | 
| 460 |  | return(0); | 
| 461 |  | fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); | 
| 462 |  | return(-1); | 
| 463 |  | } | 
| 464 | < | fputs(s, stdout);                       /* copy header info. */ | 
| 464 | > | if (!strncmp(s, "NROWS=", 6)) { | 
| 465 | > | n = atoi(s+6); | 
| 466 | > | if ((ni_rows > 0) & (n != ni_rows)) { | 
| 467 | > | fputs("Incorrect input row count\n", stderr); | 
| 468 | > | return(-1); | 
| 469 | > | } | 
| 470 | > | ni_rows = n; | 
| 471 | > | return(0); | 
| 472 | > | } | 
| 473 | > | if (!strncmp(s, "NCOLS=", 6)) { | 
| 474 | > | n = atoi(s+6); | 
| 475 | > | if ((ni_columns > 0) & (n != ni_columns)) { | 
| 476 | > | fputs("Incorrect input column count\n", stderr); | 
| 477 | > | return(-1); | 
| 478 | > | } | 
| 479 | > | ni_columns = n; | 
| 480 | > | return(0); | 
| 481 | > | } | 
| 482 | > | if (!strncmp(s, "NCOMP=", 6)) { | 
| 483 | > | n = atoi(s+6); | 
| 484 | > | if ((n_comp > 0) & (n != n_comp)) { | 
| 485 | > | fputs("Incorrect number of components\n", stderr); | 
| 486 | > | return(-1); | 
| 487 | > | } | 
| 488 | > | n_comp = n; | 
| 489 | > | return(0); | 
| 490 | > | } | 
| 491 | > | if (o_header) | 
| 492 | > | fputs(s, stdout);               /* copy header info. */ | 
| 493 |  | return(0); | 
| 494 |  | } | 
| 495 |  |  | 
| 497 |  | int | 
| 498 |  | main(int argc, char *argv[]) | 
| 499 |  | { | 
| 500 | < | int     do_header = 1;                  /* header i/o? */ | 
| 425 | < | int     transpose = 0;                  /* transpose rows & cols? */ | 
| 426 | < | int     i; | 
| 500 | > | int     a; | 
| 501 |  |  | 
| 502 | < | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 503 | < | switch (argv[i][1]) { | 
| 502 | > | for (a = 1; a < argc && argv[a][0] == '-'; a++) | 
| 503 | > | switch (argv[a][1]) { | 
| 504 |  | case 'i':                       /* input */ | 
| 505 | < | if (argv[i][2] == 'c')  /* columns */ | 
| 506 | < | ni_columns = atoi(argv[++i]); | 
| 507 | < | else if (argv[i][2] == 'r') | 
| 508 | < | ni_rows = atoi(argv[++i]); | 
| 505 | > | if (argv[a][2] == 'c')  /* columns */ | 
| 506 | > | ni_columns = atoi(argv[++a]); | 
| 507 | > | else if (argv[a][2] == 'r') | 
| 508 | > | ni_rows = atoi(argv[++a]); | 
| 509 |  | else | 
| 510 |  | goto userr; | 
| 511 |  | break; | 
| 512 |  | case 'o':                       /* output */ | 
| 513 | < | if (argv[i][2] == 'c')  /* columns */ | 
| 514 | < | no_columns = atoi(argv[++i]); | 
| 515 | < | else if (argv[i][2] == 'r') | 
| 516 | < | no_rows = atoi(argv[++i]); | 
| 513 | > | if (argv[a][2] == 'c')  /* columns */ | 
| 514 | > | no_columns = atoi(argv[++a]); | 
| 515 | > | else if (argv[a][2] == 'r') | 
| 516 | > | no_rows = atoi(argv[++a]); | 
| 517 |  | else | 
| 518 |  | goto userr; | 
| 519 |  | break; | 
| 520 | < | case 'h':                       /* header on/off */ | 
| 521 | < | do_header = !do_header; | 
| 520 | > | case 'h':                       /* turn off header */ | 
| 521 | > | switch (argv[a][2]) { | 
| 522 | > | case 'i': | 
| 523 | > | i_header = 0; | 
| 524 | > | break; | 
| 525 | > | case 'o': | 
| 526 | > | o_header = 0; | 
| 527 | > | break; | 
| 528 | > | case '\0': | 
| 529 | > | i_header = o_header = 0; | 
| 530 | > | break; | 
| 531 | > | default: | 
| 532 | > | goto userr; | 
| 533 | > | } | 
| 534 |  | break; | 
| 535 |  | case 't':                       /* transpose on/off */ | 
| 536 |  | transpose = !transpose; | 
| 537 |  | break; | 
| 538 |  | case 'f':                       /* format */ | 
| 539 | < | switch (argv[i][2]) { | 
| 539 | > | switch (argv[a][2]) { | 
| 540 |  | case 'a':               /* ASCII */ | 
| 541 |  | case 'A': | 
| 542 |  | fmtid = "ascii"; | 
| 543 | < | record_width = 1; | 
| 543 | > | comp_size = 0; | 
| 544 |  | break; | 
| 545 |  | case 'f':               /* float */ | 
| 546 |  | case 'F': | 
| 547 |  | fmtid = "float"; | 
| 548 | < | record_width = -(int)sizeof(float); | 
| 548 | > | comp_size = sizeof(float); | 
| 549 |  | break; | 
| 550 |  | case 'd':               /* double */ | 
| 551 |  | case 'D': | 
| 552 |  | fmtid = "double"; | 
| 553 | < | record_width = -(int)sizeof(double); | 
| 553 | > | comp_size = sizeof(double); | 
| 554 |  | break; | 
| 555 |  | case 'b':               /* binary (bytes) */ | 
| 556 |  | case 'B': | 
| 557 |  | fmtid = "byte"; | 
| 558 | < | record_width = -1; | 
| 558 | > | comp_size = 1; | 
| 559 |  | break; | 
| 560 |  | default: | 
| 561 |  | goto userr; | 
| 562 |  | } | 
| 563 | < | if (argv[i][3]) { | 
| 564 | < | if (!isdigit(argv[i][3])) | 
| 563 | > | if (argv[a][3]) { | 
| 564 | > | if (!isdigit(argv[a][3])) | 
| 565 |  | goto userr; | 
| 566 | < | record_width *= atoi(argv[i]+3); | 
| 567 | < | } | 
| 566 | > | n_comp = atoi(argv[a]+3); | 
| 567 | > | } else | 
| 568 | > | n_comp = 1; | 
| 569 |  | break; | 
| 570 |  | case 'w':                       /* warnings on/off */ | 
| 571 |  | warnings = !warnings; | 
| 573 |  | default: | 
| 574 |  | goto userr; | 
| 575 |  | } | 
| 576 | < | if (!record_width) | 
| 576 | > | if (a < argc-1)                         /* arg count OK? */ | 
| 577 |  | goto userr; | 
| 491 | – | if (i < argc-1)                         /* arg count OK? */ | 
| 492 | – | goto userr; | 
| 578 |  | /* open input file? */ | 
| 579 | < | if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { | 
| 580 | < | fprintf(stderr, "%s: cannot open for reading\n", argv[i]); | 
| 579 | > | if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { | 
| 580 | > | fprintf(stderr, "%s: cannot open for reading\n", argv[a]); | 
| 581 |  | return(1); | 
| 582 |  | } | 
| 583 | < | if (record_width < 0) { | 
| 583 | > | if (comp_size) { | 
| 584 |  | SET_FILE_BINARY(stdin); | 
| 585 |  | SET_FILE_BINARY(stdout); | 
| 586 |  | } | 
| 587 |  | /* check for no-op */ | 
| 588 | < | if (!transpose && (record_width < 0 || | 
| 589 | < | (no_columns == ni_columns) & (no_rows == ni_rows))) { | 
| 588 | > | if (!transpose & (i_header == o_header) && | 
| 589 | > | (no_columns == ni_columns) & (no_rows == ni_rows)) { | 
| 590 |  | if (warnings) | 
| 591 |  | fprintf(stderr, "%s: no-op -- copying input verbatim\n", | 
| 592 |  | argv[0]); | 
| 594 |  | return(1); | 
| 595 |  | return(0); | 
| 596 |  | } | 
| 597 | < | if (do_header) {                        /* read/write header */ | 
| 598 | < | if (getheader(stdin, &headline, NULL) < 0) | 
| 597 | > | if (i_header) {                         /* read header */ | 
| 598 | > | if (getheader(stdin, headline, NULL) < 0) | 
| 599 |  | return(1); | 
| 600 | < | printargs(argc, argv, stdout); | 
| 600 | > | if (!check_sizes()) | 
| 601 | > | return(1); | 
| 602 | > | if (comp_size) {                /* a little late... */ | 
| 603 | > | SET_FILE_BINARY(stdin); | 
| 604 | > | SET_FILE_BINARY(stdout); | 
| 605 | > | } | 
| 606 | > | } else if (!check_sizes()) | 
| 607 | > | return(1); | 
| 608 | > | if (o_header) {                         /* write/add to header */ | 
| 609 | > | if (!i_header) | 
| 610 | > | newheader("RADIANCE", stdout); | 
| 611 | > | printargs(a, argv, stdout); | 
| 612 | > | if (no_rows > 0) | 
| 613 | > | printf("NROWS=%d\n", no_rows); | 
| 614 | > | if (no_columns > 0) | 
| 615 | > | printf("NCOLS=%d\n", no_columns); | 
| 616 | > | printf("NCOMP=%d\n", n_comp); | 
| 617 |  | fputformat(fmtid, stdout); | 
| 618 |  | fputc('\n', stdout);            /* finish new header */ | 
| 619 |  | } | 
| 620 |  | if (transpose) {                        /* transposing rows & columns? */ | 
| 621 | < | MEMLOAD myMem;                  /* need to load into memory */ | 
| 622 | < | if (i == argc-1) { | 
| 621 | > | MEMLOAD myMem;                  /* need to map into memory */ | 
| 622 | > | if (a == argc-1) { | 
| 623 |  | if (load_file(&myMem, stdin) <= 0) { | 
| 624 |  | fprintf(stderr, "%s: error loading file into memory\n", | 
| 625 | < | argv[i]); | 
| 625 | > | argv[a]); | 
| 626 |  | return(1); | 
| 627 |  | } | 
| 628 |  | } else if (load_stream(&myMem, stdin) <= 0) { | 
| 632 |  | } | 
| 633 |  | if (!do_transpose(&myMem)) | 
| 634 |  | return(1); | 
| 635 | < | /* free_load(&myMem); */ | 
| 636 | < | } else if (!do_resize(stdin))           /* just reshaping input */ | 
| 635 | > | /* free_load(&myMem);   about to exit, so don't bother */ | 
| 636 | > | } else if (!do_resize(stdin))           /* reshaping input */ | 
| 637 |  | return(1); | 
| 638 |  | return(0); | 
| 639 |  | userr: | 
| 640 |  | fprintf(stderr, | 
| 641 | < | "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 641 | > | "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 642 |  | argv[0]); | 
| 643 |  | return(1); | 
| 644 |  | } |