ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
(Generate patch)

Comparing ray/src/util/rcollate.c (file contents):
Revision 2.1 by greg, Thu Sep 5 17:53:23 2013 UTC vs.
Revision 2.39 by greg, Thu Mar 3 03:55:13 2022 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
9   #include <ctype.h>
10   #include "platform.h"
11   #include "rtio.h"
12   #include "resolu.h"
13 < #ifndef _WIN32
14 < #include <sys/mman.h>
13 > #if defined(_WIN32) || defined(_WIN64)
14 >  #undef ftello
15 >  #define       ftello  ftell
16 >  #undef ssize_t
17 >  #define ssize_t       size_t
18 > #else
19 >  #include <sys/mman.h>
20   #endif
21  
22 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
20 < #undef getc
21 < #undef putc
22 < #define getc    getc_unlocked
23 < #define putc    putc_unlocked
24 < #endif
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 */
29        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 >        ssize_t nrecs;          /* number of records we found */
33          char    *rec[1];        /* record array (extends struct) */
34   } RECINDEX;
35  
36 + int             warnings = 1;   /* report warnings? */
37 +
38   /* free loaded file */
39   static void
40   free_load(MEMLOAD *mp)
# Line 43 | Line 43 | free_load(MEMLOAD *mp)
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   }
54  
55 + /* load memory from an input stream, starting from current position */
56 + static int
57 + load_stream(MEMLOAD *mp, FILE *fp)
58 + {
59 +        size_t  alloced = 0;
60 +        char    buf[8192];
61 +        size_t  nr;
62 +
63 +        if (mp == NULL)
64 +                return(-1);
65 +        mp->mapped = NULL;
66 +        mp->base = NULL;
67 +        mp->len = 0;
68 +        if (fp == NULL)
69 +                return(-1);
70 +        while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
71 +                if (!alloced)
72 +                        mp->base = malloc(alloced = nr);
73 +                else if (mp->len+nr > alloced)
74 +                        mp->base = realloc(mp->base,
75 +                                alloced = alloced*(2+(nr==sizeof(buf)))/2+nr);
76 +                if (mp->base == NULL)
77 +                        return(-1);
78 +                memcpy((char *)mp->base + mp->len, buf, nr);
79 +                mp->len += nr;
80 +        }
81 +        if (ferror(fp)) {
82 +                free_load(mp);
83 +                return(-1);
84 +        }
85 +        if (alloced > mp->len*5/4)      /* don't waste too much space */
86 +                mp->base = realloc(mp->base, mp->len);
87 +        return(mp->len > 0);
88 + }
89 +
90 + #if defined(_WIN32) || defined(_WIN64)
91 +                                /* too difficult to fix this */
92 + #define load_file       load_stream
93 + #else
94   /* load a file into memory */
95   static int
96   load_file(MEMLOAD *mp, FILE *fp)
97   {
98          int     fd;
99 <        off_t   skip, flen;
60 <
99 >        off_t   skip, flen, fpos;
100          if (mp == NULL)
101                  return(-1);
102 +        mp->mapped = NULL;
103          mp->base = NULL;
104          mp->len = 0;
65        mp->mapped = 0;
105          if (fp == NULL)
106                  return(-1);
107          fd = fileno(fp);
# Line 72 | Line 111 | load_file(MEMLOAD *mp, FILE *fp)
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->base = mmap(NULL, mp->len, PROT_READ|PROT_WRITE,
116 <                                                MAP_PRIVATE, fd, skip);
117 <                if (mp->base != MAP_FAILED) {
79 <                        mp->mapped = 1;
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->base = NULL;        /* fall back to reading it in... */
120 >                mp->mapped = NULL;      /* else fall back to reading it in... */
121          }
122   #endif
123          if (lseek(fd, skip, SEEK_SET) != skip ||
# Line 87 | Line 125 | load_file(MEMLOAD *mp, FILE *fp)
125                  mp->len = 0;
126                  return(-1);
127          }
128 <        if (read(fd, (char *)mp->base, mp->len) != mp->len) {
129 <                free_load(mp);
130 <                return(-1);
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 + #endif
141  
97 /* load memory from an input stream, starting from current position */
98 static int
99 load_stream(MEMLOAD *mp, FILE *fp)
100 {
101        char    buf[8192];
102        size_t  nr;
103
104        if (mp == NULL)
105                return(-1);
106        mp->base = NULL;
107        mp->len = 0;
108        mp->mapped = 0;
109        if (fp == NULL)
110                return(-1);
111        while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
112                if (!mp->len)
113                        mp->base = malloc(nr);
114                else
115                        mp->base = realloc(mp->base, mp->len+nr);
116                if (mp->base == NULL)
117                        return(-1);
118                memcpy((char *)mp->base + mp->len, buf, nr);
119                mp->len += nr;
120        }
121        if (ferror(fp)) {
122                free_load(mp);
123                return(-1);
124        }
125        return(mp->len > 0);
126 }
127
142   /* free a record index */
143   #define free_records(rp)        free(rp)
144  
# Line 132 | Line 146 | load_stream(MEMLOAD *mp, FILE *fp)
146   static RECINDEX *
147   index_records(const MEMLOAD *mp, int nw_rec)
148   {
149 +        int             nall = 0;
150          RECINDEX        *rp;
151          char            *cp, *mend;
152          int             n;
# Line 140 | Line 155 | index_records(const MEMLOAD *mp, int nw_rec)
155                  return(NULL);
156          if (nw_rec <= 0)
157                  return(NULL);
158 <        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *));
158 >        nall = 1000;
159 >        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *));
160          if (rp == NULL)
161                  return(NULL);
162          rp->nw_rec = nw_rec;
# Line 152 | Line 168 | index_records(const MEMLOAD *mp, int nw_rec)
168                          ++cp;
169                  if (cp >= mend)
170                          break;
171 +                if (rp->nrecs >= nall) {
172 +                        nall += nall>>1;        /* get more record space */
173 +                        rp = (RECINDEX *)realloc(rp,
174 +                                        sizeof(RECINDEX) + nall*sizeof(char *));
175 +                        if (rp == NULL)
176 +                                return(NULL);
177 +                }
178                  rp->rec[rp->nrecs++] = cp;      /* point to first non-white */
179                  n = rp->nw_rec;
180                  while (++cp < mend)             /* find end of record */
# Line 195 | Line 218 | count_columns(const RECINDEX *rp)
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, ssize_t n)
222   {
223          int     words2go = rp->nw_rec;
224          char    *scp;
# Line 228 | Line 251 | output_stream(FILE *fp)
251  
252          if (fp == NULL)
253                  return(0);
254 <        fflush(stdout);                 /* assumes nothing in input buffer */
255 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
254 >        fflush(stdout);
255 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
256                  if (write(fileno(stdout), buf, n) != n)
257                          return(0);
258 <        return(n >= 0);
258 >        return(!ferror(fp));
259   }
260  
261   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 258 | Line 281 | fget_word(char buf[256], FILE *fp)
281          return(buf);
282   }
283  
284 < char            *fmtid = "ascii";               /* format id */
285 < int             record_width = 3;               /* words/record (<0 binary) */
284 > char            *fmtid = NULL;                  /* format id */
285 > int             comp_size = 0;                  /* binary bytes/channel */
286 > int             n_comp = 0;                     /* components/record */
287   int             ni_columns = 0;                 /* number of input columns */
288   int             ni_rows = 0;                    /* number of input rows */
289   int             no_columns = 0;                 /* number of output columns */
290   int             no_rows = 0;                    /* number of output rows */
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 + int             check = 0;                      /* force data check? */
297  
298 < /* output transposed ASCII or binary data from memory */
298 > /* parse RxCx... string */
299   static int
300 < do_transpose(const MEMLOAD *mp)
300 > get_array(const char *spec, int blklvl[][2], int nlvls)
301   {
302 +        int     n;
303 +
304 +        if (nlvls <= 0) {
305 +                fputs("Too many block levels!\n", stderr);
306 +                exit(1);
307 +        }
308 +        if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) {
309 +                fputs("Bad block specification!\n", stderr);
310 +                exit(1);
311 +        }
312 +        while (isdigit(*spec))
313 +                spec++;
314 +        spec++;         /* 'x' */
315 +        while (isdigit(*spec))
316 +                spec++;
317 +        if ((*spec != 'x') & (*spec != 'X')) {
318 +                if (*spec) {
319 +                        fputs("Blocks must be separated by 'x' or 'X'\n", stderr);
320 +                        exit(1);
321 +                }
322 +                return(1);
323 +        }
324 +        spec++;
325 +        n = get_array(spec, blklvl+1, nlvls-1);
326 +        if (!n)
327 +                return(0);
328 +        blklvl[0][0] *= blklvl[1][0];
329 +        blklvl[0][1] *= blklvl[1][1];
330 +        return(n+1);
331 + }
332 +
333 + /* check settings and assign defaults */
334 + static int
335 + check_sizes()
336 + {
337 +        if (fmtid == NULL) {
338 +                fmtid = "ascii";
339 +        } else if (!comp_size) {
340 +                if (!strcmp(fmtid, "float"))
341 +                        comp_size = sizeof(float);
342 +                else if (!strcmp(fmtid, "double"))
343 +                        comp_size = sizeof(double);
344 +                else if (!strcmp(fmtid, "byte"))
345 +                        comp_size = 1;
346 +                else if (strcmp(fmtid, "ascii")) {
347 +                        fprintf(stderr, "Unsupported format: %s\n", fmtid);
348 +                        return(0);
349 +                }
350 +        }
351 +        if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
352 +                if (ni_rows > 0) no_columns = ni_rows;
353 +                if (ni_columns > 0) no_rows = ni_columns;
354 +        } else if ((no_rows <= 0) & (no_columns > 0) &&
355 +                        !((ni_rows*ni_columns) % no_columns))
356 +                no_rows = ni_rows*ni_columns/no_columns;
357 +        if (n_comp <= 0)
358 +                n_comp = 3;
359 +        return(1);
360 + }
361 +
362 + /* call to compute block input position */
363 + static ssize_t
364 + get_block_pos(int r, int c, int blklvl[][2], int nlvls)
365 + {
366 +        ssize_t n = 0;
367 +
368 +        while (nlvls > 1) {
369 +                int     sr = r/blklvl[1][0];
370 +                int     sc = c/blklvl[1][1];
371 +                r -= sr*blklvl[1][0];
372 +                c -= sc*blklvl[1][1];
373 +                n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1];
374 +                blklvl++;
375 +                nlvls--;
376 +        }
377 +        n += r*blklvl[0][1] + c;
378 +        return(n);
379 + }
380 +
381 + /* return input offset based on array ordering and transpose option */
382 + static ssize_t
383 + get_input_pos(int r, int c)
384 + {
385 +        ssize_t n;
386 +
387 +        if (outLevels > 1) {            /* block reordering */
388 +                n = get_block_pos(r, c, outArray, outLevels);
389 +                if (transpose) {
390 +                        r = n/ni_rows;
391 +                        c = n - r*ni_rows;
392 +                        n = (ssize_t)c*ni_columns + r;
393 +                }
394 +        } else if (transpose)           /* transpose only */
395 +                n = (ssize_t)c*ni_columns + r;
396 +        else                            /* XXX should never happen! */
397 +                n = (ssize_t)r*no_columns + c;
398 +        return(n);
399 + }
400 +
401 + /* output reordered ASCII or binary data from memory */
402 + static int
403 + do_reorder(const MEMLOAD *mp)
404 + {
405          static const char       tabEOL[2] = {'\t','\n'};
406          RECINDEX                *rp = NULL;
407 <        long                    nrecords;
407 >        ssize_t                 nrecords;
408          int                     i, j;
409 +                                                /* propogate sizes */
410 +        if (ni_rows <= 0)
411 +                ni_rows = transpose ? no_columns : no_rows;
412 +        if (ni_columns <= 0)
413 +                ni_columns = transpose ? no_rows : no_columns;
414                                                  /* get # records (& index) */
415 <        if (record_width > 0) {
416 <                if ((rp = index_records(mp, record_width)) == NULL)
415 >        if (!comp_size) {
416 >                if ((rp = index_records(mp, n_comp)) == NULL)
417                          return(0);
418                  if (ni_columns <= 0)
419                          ni_columns = count_columns(rp);
420                  nrecords = rp->nrecs;
421 <        } else if ((ni_rows > 0) & (ni_columns > 0))
422 <                nrecords = ni_rows*ni_columns;
423 <        else
424 <                nrecords = mp->len / -record_width;
421 >        } else if ((ni_rows > 0) & (ni_columns > 0)) {
422 >                nrecords = (ssize_t)ni_rows*ni_columns;
423 >                if (nrecords > mp->len/(n_comp*comp_size)) {
424 >                        fputs("Input too small for specified size and type\n",
425 >                                        stderr);
426 >                        return(0);
427 >                }
428 >        } else
429 >                nrecords = mp->len/(n_comp*comp_size);
430                                                  /* check sizes */
288        if (ni_rows <= 0)
289                ni_rows = no_columns;
290        if (ni_columns <= 0)
291                ni_columns = no_rows;
431          if ((ni_rows <= 0) & (ni_columns > 0))
432                  ni_rows = nrecords/ni_columns;
433 <        if ((ni_columns <= 0) & (ni_rows > 0))
433 >        else if ((ni_columns <= 0) & (ni_rows > 0))
434                  ni_columns = nrecords/ni_rows;
435 <        if (nrecords != ni_rows*ni_columns)
435 >        if (nrecords != (ssize_t)ni_rows*ni_columns)
436                  goto badspec;
437 <        if (no_columns <= 0)
438 <                no_columns = ni_rows;
439 <        if (no_rows <= 0)
440 <                no_rows = ni_columns;
441 <        if ((no_rows != ni_columns) | (no_columns != ni_rows))
442 <                goto badspec;
443 <                                                /* transpose records */
437 >        if (transpose) {
438 >                if (no_columns <= 0)
439 >                        no_columns = ni_rows;
440 >                if (no_rows <= 0)
441 >                        no_rows = ni_columns;
442 >                if (outLevels <= 1 &&
443 >                                (no_rows != ni_columns) | (no_columns != ni_rows))
444 >                        goto badspec;
445 >        } else {
446 >                if (no_columns <= 0)
447 >                        no_columns = ni_columns;
448 >                if (no_rows <= 0)
449 >                        no_rows = ni_rows;
450 >        }
451 >        if (ni_rows*ni_columns != no_rows*no_columns) {
452 >                fputs("Number of input and output records do not match\n",
453 >                                stderr);
454 >                return(0);
455 >        }
456 >        if (o_header) {                         /* finish header? */
457 >                printf("NROWS=%d\n", no_rows);
458 >                printf("NCOLS=%d\n", no_columns);
459 >                fputformat(fmtid, stdout);
460 >                fputc('\n', stdout);
461 >        }
462 >                                                /* reorder records */
463          for (i = 0; i < no_rows; i++) {
464 <            for (j = 0; j < no_columns; j++)
464 >            for (j = 0; j < no_columns; j++) {
465 >                ssize_t n = get_input_pos(i, j);
466 >                if (n >= nrecords) {
467 >                        fputs("Index past end-of-file\n", stderr);
468 >                        return(0);
469 >                }
470                  if (rp != NULL) {               /* ASCII output */
471 <                        print_record(rp, j*ni_columns + i);
471 >                        print_record(rp, n);
472                          putc(tabEOL[j >= no_columns-1], stdout);
473                  } else {                        /* binary output */
474 <                        fwrite((char *)mp->base +
475 <                                        -record_width*(j*ni_columns + i),
313 <                                        -record_width, 1, stdout);
474 >                        putbinary((char *)mp->base + (n_comp*comp_size)*n,
475 >                                        comp_size, n_comp, stdout);
476                  }
477 +            }
478              if (ferror(stdout)) {
479                  fprintf(stderr, "Error writing to stdout\n");
480                  return(0);
# Line 321 | Line 484 | do_transpose(const MEMLOAD *mp)
484                  free_records(rp);
485          return(1);
486   badspec:
487 <        fprintf(stderr, "Bad transpose specification -- check dimension(s)\n");
487 >        fprintf(stderr, "Bad dimension(s)\n");
488          return(0);
489   }
490  
# Line 329 | Line 492 | badspec:
492   static int
493   do_resize(FILE *fp)
494   {
495 <        long    records2go = ni_rows*ni_columns;
495 >        ssize_t records2go = ni_rows*ni_columns;
496          int     columns2go = no_columns;
497          char    word[256];
498 <                                                /* sanity checks */
499 <        if (record_width <= 0) {
500 <                fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
501 <                                record_width);
502 <                return(0);
498 >                        
499 >        if (o_header) {                         /* finish header? */
500 >                if (no_rows > 0)
501 >                        printf("NROWS=%d\n", no_rows);
502 >                if (no_columns > 0)
503 >                        printf("NCOLS=%d\n", no_columns);
504 >                fputformat(fmtid, stdout);
505 >                fputc('\n', stdout);
506          }
507 +                                                /* sanity checks */
508 +        if (comp_size || !check &
509 +                        (no_columns == ni_columns) & (no_rows == ni_rows))
510 +                return(output_stream(fp));      /* no-op -- just copy */
511          if (no_columns <= 0) {
512                  fprintf(stderr, "Missing -oc specification\n");
513                  return(0);
# Line 346 | Line 516 | do_resize(FILE *fp)
516                  records2go = no_rows*no_columns;
517          else if (no_rows*no_columns != records2go) {
518                  fprintf(stderr,
519 <                        "Input and output data sizes disagree (%dx%d != %dx%d)\n",
519 >                        "Number of input and output records disagree (%dx%d != %dx%d)\n",
520                                  ni_rows, ni_columns, no_rows, no_columns);
521                  return(0);
522          }
523          do {                                    /* reshape records */
524                  int     n;
525  
526 <                for (n = record_width; n--; ) {
526 >                for (n = n_comp; n--; ) {
527                          if (fget_word(word, fp) == NULL) {
528 <                                if (records2go > 0 || n < record_width-1)
528 >                                if (records2go > 0 || n < n_comp-1)
529                                          break;
530                                  goto done;      /* normal EOD */
531                          }
# Line 379 | Line 549 | do_resize(FILE *fp)
549                          putc('\t', stdout);
550          } while (--records2go);                 /* expected EOD? */
551   done:
552 <        if (columns2go != no_columns)
552 >        if (warnings && columns2go != no_columns)
553                  fprintf(stderr, "Warning -- incomplete final row\n");
554 <        if (fget_word(word, fp) != NULL)
555 <                fprintf(stderr, "Warning -- data beyond expected EOF\n");
554 >        if (warnings && fget_word(word, fp) != NULL)
555 >                fprintf(stderr, "Warning -- characters beyond expected EOD\n");
556          return(1);
557   }
558  
# Line 390 | Line 560 | done:
560   static int
561   headline(char *s, void *p)
562   {
563 <        char    fmt[32];
563 >        static char     fmt[MAXFMTLEN];
564 >        int             n;
565  
566          if (formatval(fmt, s)) {
567 +                if (fmtid == NULL) {
568 +                        fmtid = fmt;
569 +                        return(0);
570 +                }
571                  if (!strcmp(fmt, fmtid))
572                          return(0);
573                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
574                  return(-1);
575          }
576 <        fputs(s, stdout);                       /* copy header info. */
576 >        if (!strncmp(s, "NROWS=", 6)) {
577 >                n = atoi(s+6);
578 >                if ((ni_rows > 0) & (n != ni_rows)) {
579 >                        fputs("Incorrect input row count\n", stderr);
580 >                        return(-1);
581 >                }
582 >                ni_rows = n;
583 >                return(0);
584 >        }
585 >        if (!strncmp(s, "NCOLS=", 6)) {
586 >                n = atoi(s+6);
587 >                if ((ni_columns > 0) & (n != ni_columns)) {
588 >                        fputs("Incorrect input column count\n", stderr);
589 >                        return(-1);
590 >                }
591 >                ni_columns = n;
592 >                return(0);
593 >        }
594 >        if (!strncmp(s, "NCOMP=", 6)) {
595 >                n = atoi(s+6);
596 >                if ((n_comp > 0) & (n != n_comp)) {
597 >                        fputs("Incorrect number of components\n", stderr);
598 >                        return(-1);
599 >                }
600 >                n_comp = n;
601 >                return(0);
602 >        }
603 >        if (o_header)
604 >                fputs(s, stdout);               /* copy header info. */
605          return(0);
606   }
607  
# Line 406 | Line 609 | headline(char *s, void *p)
609   int
610   main(int argc, char *argv[])
611   {
612 <        int     do_header = 1;                  /* header i/o? */
410 <        int     transpose = 0;                  /* transpose rows & cols? */
411 <        int     i;
612 >        int     a;
613  
614 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
615 <                switch (argv[i][1]) {
614 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
615 >                switch (argv[a][1]) {
616                  case 'i':                       /* input */
617 <                        if (argv[i][2] == 'c')  /* columns */
618 <                                ni_columns = atoi(argv[++i]);
619 <                        else if (argv[i][2] == 'r')
620 <                                ni_rows = atoi(argv[++i]);
617 >                        if (argv[a][2] == 'c')  /* columns */
618 >                                ni_columns = atoi(argv[++a]);
619 >                        else if (argv[a][2] == 'r')
620 >                                ni_rows = atoi(argv[++a]);
621                          else
622                                  goto userr;
623                          break;
624                  case 'o':                       /* output */
625 <                        if (argv[i][2] == 'c')  /* columns */
626 <                                no_columns = atoi(argv[++i]);
627 <                        else if (argv[i][2] == 'r')
628 <                                no_rows = atoi(argv[++i]);
629 <                        else
625 >                        if (argv[a][2] == 'c')  /* columns */
626 >                                no_columns = atoi(argv[++a]);
627 >                        else if (argv[a][2] == 'r')
628 >                                no_rows = atoi(argv[++a]);
629 >                        else if (argv[a][2] ||
630 >                          !(outLevels=get_array(argv[++a], outArray, MAXLEVELS)))
631                                  goto userr;
632                          break;
633 <                case 'h':                       /* header on/off */
634 <                        do_header = !do_header;
633 >                case 'h':                       /* turn off header */
634 >                        switch (argv[a][2]) {
635 >                        case 'i':
636 >                                i_header = 0;
637 >                                break;
638 >                        case 'o':
639 >                                o_header = 0;
640 >                                break;
641 >                        case '\0':
642 >                                i_header = o_header = 0;
643 >                                break;
644 >                        default:
645 >                                goto userr;
646 >                        }
647                          break;
648                  case 't':                       /* transpose on/off */
649                          transpose = !transpose;
650                          break;
651                  case 'f':                       /* format */
652 <                        switch (argv[i][2]) {
652 >                        switch (argv[a][2]) {
653                          case 'a':               /* ASCII */
654                          case 'A':
655                                  fmtid = "ascii";
656 <                                record_width = 1;
656 >                                comp_size = 0;
657                                  break;
658                          case 'f':               /* float */
659                          case 'F':
660                                  fmtid = "float";
661 <                                record_width = -(int)sizeof(float);
661 >                                comp_size = sizeof(float);
662                                  break;
663                          case 'd':               /* double */
664                          case 'D':
665                                  fmtid = "double";
666 <                                record_width = -(int)sizeof(double);
666 >                                comp_size = sizeof(double);
667                                  break;
668                          case 'b':               /* binary (bytes) */
669                          case 'B':
670                                  fmtid = "byte";
671 <                                record_width = -1;
671 >                                comp_size = 1;
672                                  break;
673                          default:
674                                  goto userr;
675                          }
676 <                        if (argv[i][3]) {
677 <                                if (!isdigit(argv[i][3]))
676 >                        if (argv[a][3]) {
677 >                                if (!isdigit(argv[a][3]))
678                                          goto userr;
679 <                                record_width *= atoi(argv[i]+3);
680 <                        }
679 >                                n_comp = atoi(argv[a]+3);
680 >                        } else
681 >                                n_comp = 1;
682                          break;
683 +                case 'w':                       /* warnings on/off */
684 +                        warnings = !warnings;
685 +                        break;
686 +                case 'c':                       /* force check operation */
687 +                        check = 1;
688 +                        break;
689                  default:
690                          goto userr;
691                  }
692 <        if (!record_width)
692 >        if (a < argc-1)                         /* arg count OK? */
693                  goto userr;
694 <        if (i < argc-1)                         /* arg count OK? */
695 <                goto userr;
694 >        if (outLevels) {                        /* should check consistency? */
695 >                no_rows = outArray[0][0];
696 >                no_columns = outArray[0][1];
697 >        }
698                                                  /* open input file? */
699 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
700 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
699 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
700 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
701                  return(1);
702          }
703 <        if (record_width < 0) {
703 >        if (comp_size) {
704                  SET_FILE_BINARY(stdin);
705                  SET_FILE_BINARY(stdout);
706          }
707                                                  /* check for no-op */
708 <        if (!transpose && (record_width < 0 ||
709 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
710 <                fprintf(stderr, "%s: no-op -- copying input verbatim\n",
708 >        if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) &&
709 >                        (no_columns == ni_columns) & (no_rows == ni_rows)) {
710 >                if (warnings)
711 >                        fprintf(stderr, "%s: no-op -- copying input verbatim\n",
712                                  argv[0]);
713                  if (!output_stream(stdin))
714                          return(1);
715                  return(0);
716          }
717 <        if (do_header) {                        /* read/write header */
718 <                if (getheader(stdin, &headline, NULL) < 0)
717 >        if (i_header) {                         /* read header */
718 >                if (getheader(stdin, headline, NULL) < 0)
719                          return(1);
720 <                printargs(argc, argv, stdout);
721 <                fputformat(fmtid, stdout);
722 <                fputc('\n', stdout);            /* finish new header */
720 >                if (!check_sizes())
721 >                        return(1);
722 >                if (comp_size) {                /* a little late... */
723 >                        SET_FILE_BINARY(stdin);
724 >                        SET_FILE_BINARY(stdout);
725 >                }
726 >        } else if (!check_sizes())
727 >                return(1);
728 >        if (o_header) {                         /* write/add to header */
729 >                if (!i_header)
730 >                        newheader("RADIANCE", stdout);
731 >                printargs(a, argv, stdout);
732 >                printf("NCOMP=%d\n", n_comp);
733          }
734 <        if (transpose) {                        /* transposing rows & columns? */
735 <                MEMLOAD myMem;                  /* need to load into memory */
736 <                if (i == argc-1) {
734 >        if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) {
735 >                MEMLOAD myMem;                  /* need to map into memory */
736 >                if (a == argc-1) {
737                          if (load_file(&myMem, stdin) <= 0) {
738                                  fprintf(stderr, "%s: error loading file into memory\n",
739 <                                                argv[i]);
739 >                                                argv[a]);
740                                  return(1);
741                          }
742                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 510 | Line 744 | main(int argc, char *argv[])
744                                                  argv[0]);
745                          return(1);
746                  }
747 <                if (!do_transpose(&myMem))
747 >                if (!do_reorder(&myMem))
748                          return(1);
749 <                /* free_load(&myMem); */
749 >                /* free_load(&myMem);   about to exit, so don't bother */
750          } else if (!do_resize(stdin))           /* just reshaping input */
751                  return(1);
752          return(0);
753   userr:
754          fprintf(stderr,
755 < "Usage: %s [-h][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
755 > "Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n",
756                          argv[0]);
757          return(1);
758   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines