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.12 by greg, Fri May 30 18:10:20 2014 UTC vs.
Revision 2.32 by greg, Fri Nov 8 05:39:05 2019 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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
23 > #define MAXLEVELS       16      /* max RxC.. block pairs */
24  
25   typedef struct {
26 +        void    *mapped;        /* memory-mapped pointer */
27          void    *base;          /* pointer to base memory */
28          size_t  len;            /* allocated memory length */
33        int     mapped;         /* memory-mapped file? */
29   } MEMLOAD;              /* file loaded/mapped into memory */
30  
31   typedef struct {
32          int     nw_rec;         /* number of words per record */
33 <        int     nrecs;          /* number of records we found */
33 >        long    nrecs;          /* number of records we found */
34          char    *rec[1];        /* record array (extends struct) */
35   } RECINDEX;
36  
# Line 49 | Line 44 | free_load(MEMLOAD *mp)
44                  return;
45   #ifdef MAP_FILE
46          if (mp->mapped)
47 <                munmap(mp->base, mp->len);
47 >                munmap(mp->mapped, mp->len);
48          else
49   #endif
50                  free(mp->base);
51 +        mp->mapped = NULL;
52          mp->base = NULL;
53          mp->len = 0;
54   }
55  
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
56   /* load memory from an input stream, starting from current position */
57   static int
58   load_stream(MEMLOAD *mp, FILE *fp)
# Line 109 | Line 63 | load_stream(MEMLOAD *mp, FILE *fp)
63  
64          if (mp == NULL)
65                  return(-1);
66 +        mp->mapped = NULL;
67          mp->base = NULL;
68          mp->len = 0;
114        mp->mapped = 0;
69          if (fp == NULL)
70                  return(-1);
71          while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
72                  if (!alloced)
73 <                        mp->base = malloc(nr);
73 >                        mp->base = malloc(alloced = nr);
74                  else if (mp->len+nr > alloced)
75                          mp->base = realloc(mp->base,
76                                  alloced = alloced*(2+(nr==sizeof(buf)))/2+nr);
# Line 134 | Line 88 | load_stream(MEMLOAD *mp, FILE *fp)
88          return(mp->len > 0);
89   }
90  
91 + /* load a file into memory */
92 + static int
93 + load_file(MEMLOAD *mp, FILE *fp)
94 + {
95 +        int     fd;
96 +        off_t   skip, flen, fpos;
97 +
98 + #if defined(_WIN32) || defined(_WIN64)
99 +                                /* too difficult to fix this */
100 +        return load_stream(mp, fp);
101 + #endif
102 +        if (mp == NULL)
103 +                return(-1);
104 +        mp->mapped = NULL;
105 +        mp->base = NULL;
106 +        mp->len = 0;
107 +        if (fp == NULL)
108 +                return(-1);
109 +        fd = fileno(fp);
110 +        skip = ftello(fp);
111 +        flen = lseek(fd, 0, SEEK_END);
112 +        if (flen <= skip)
113 +                return((int)(flen - skip));
114 +        mp->len = (size_t)(flen - skip);
115 + #ifdef MAP_FILE
116 +        if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
117 +                mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
118 +                if (mp->mapped != MAP_FAILED) {
119 +                        mp->base = (char *)mp->mapped + skip;
120 +                        return(1);      /* mmap() success */
121 +                }
122 +                mp->mapped = NULL;      /* else fall back to reading it in... */
123 +        }
124 + #endif
125 +        if (lseek(fd, skip, SEEK_SET) != skip ||
126 +                        (mp->base = malloc(mp->len)) == NULL) {
127 +                mp->len = 0;
128 +                return(-1);
129 +        }
130 +        fpos = skip;
131 +        while (fpos < flen) {           /* read() fails if n > 2 GBytes */
132 +                ssize_t nread = read(fd, (char *)mp->base+(fpos-skip),
133 +                                (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24);
134 +                if (nread <= 0) {
135 +                        free_load(mp);
136 +                        return(-1);
137 +                }
138 +                fpos += nread;
139 +        }
140 +        return(1);
141 + }
142 +
143   /* free a record index */
144   #define free_records(rp)        free(rp)
145  
# Line 141 | Line 147 | load_stream(MEMLOAD *mp, FILE *fp)
147   static RECINDEX *
148   index_records(const MEMLOAD *mp, int nw_rec)
149   {
150 +        int             nall = 0;
151          RECINDEX        *rp;
152          char            *cp, *mend;
153          int             n;
# Line 149 | Line 156 | index_records(const MEMLOAD *mp, int nw_rec)
156                  return(NULL);
157          if (nw_rec <= 0)
158                  return(NULL);
159 <        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *));
159 >        nall = 1000;
160 >        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *));
161          if (rp == NULL)
162                  return(NULL);
163          rp->nw_rec = nw_rec;
# Line 161 | Line 169 | index_records(const MEMLOAD *mp, int nw_rec)
169                          ++cp;
170                  if (cp >= mend)
171                          break;
172 +                if (rp->nrecs >= nall) {
173 +                        nall += nall>>1;        /* get more record space */
174 +                        rp = (RECINDEX *)realloc(rp,
175 +                                        sizeof(RECINDEX) + nall*sizeof(char *));
176 +                        if (rp == NULL)
177 +                                return(NULL);
178 +                }
179                  rp->rec[rp->nrecs++] = cp;      /* point to first non-white */
180                  n = rp->nw_rec;
181                  while (++cp < mend)             /* find end of record */
# Line 204 | Line 219 | count_columns(const RECINDEX *rp)
219  
220   /* copy nth record from index to stdout */
221   static int
222 < print_record(const RECINDEX *rp, int n)
222 > print_record(const RECINDEX *rp, long n)
223   {
224          int     words2go = rp->nw_rec;
225          char    *scp;
# Line 237 | Line 252 | output_stream(FILE *fp)
252  
253          if (fp == NULL)
254                  return(0);
255 <        fflush(stdout);                 /* assumes nothing in input buffer */
256 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
255 >        fflush(stdout);
256 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
257                  if (write(fileno(stdout), buf, n) != n)
258                          return(0);
259 <        return(n >= 0);
259 >        return(!ferror(fp));
260   }
261  
262   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 274 | Line 289 | int            ni_columns = 0;                 /* number of input columns */
289   int             ni_rows = 0;                    /* number of input rows */
290   int             no_columns = 0;                 /* number of output columns */
291   int             no_rows = 0;                    /* number of output rows */
292 + int             transpose = 0;                  /* transpose rows & cols? */
293 + int             i_header = 1;                   /* input header? */
294 + int             o_header = 1;                   /* output header? */
295 + int             outArray[MAXLEVELS][2];         /* output block nesting */
296 + int             outLevels = 0;                  /* number of blocking levels */
297  
298 + /* parse RxCx... string */
299 + static int
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()
# Line 288 | Line 343 | check_sizes()
343                          comp_size = sizeof(double);
344                  else if (!strcmp(fmtid, "byte"))
345                          comp_size = 1;
346 <                else {
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 < /* output transposed ASCII or binary data from memory */
362 > /* call to compute block input position */
363 > static long
364 > get_block_pos(int r, int c, int blklvl[][2], int nlvls)
365 > {
366 >        long    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 long
383 > get_input_pos(int r, int c)
384 > {
385 >        long    n;
386 >
387 >        if (outLevels > 1) {            /* block reordering */
388 >                n = get_block_pos(r, c, outArray, outLevels);
389 >                if (transpose) {
390 >                        r = n/no_columns;
391 >                        c = n - r*no_columns;
392 >                        n = (long)r*ni_columns + c;
393 >                }
394 >        } else if (transpose)           /* transpose only */
395 >                n = (long)c*ni_columns + r;
396 >        else                            /* XXX should never happen! */
397 >                n = (long)r*no_columns + c;
398 >        return(n);
399 > }
400 >
401 > /* output reordered ASCII or binary data from memory */
402   static int
403 < do_transpose(const MEMLOAD *mp)
403 > do_reorder(const MEMLOAD *mp)
404   {
405          static const char       tabEOL[2] = {'\t','\n'};
406          RECINDEX                *rp = NULL;
# Line 334 | Line 434 | do_transpose(const MEMLOAD *mp)
434                  ni_columns = nrecords/ni_rows;
435          if (nrecords != 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 ((no_rows != ni_columns) | (no_columns != ni_rows))
443 >                        goto badspec;
444 >        }
445 >                                                /* reorder records */
446          for (i = 0; i < no_rows; i++) {
447 <            for (j = 0; j < no_columns; j++)
447 >            for (j = 0; j < no_columns; j++) {
448 >                long    n = get_input_pos(i, j);
449                  if (rp != NULL) {               /* ASCII output */
450 <                        print_record(rp, j*ni_columns + i);
450 >                        print_record(rp, n);
451                          putc(tabEOL[j >= no_columns-1], stdout);
452                  } else {                        /* binary output */
453 <                        fwrite((char *)mp->base +
454 <                                        (n_comp*comp_size)*(j*ni_columns + i),
352 <                                        n_comp*comp_size, 1, stdout);
453 >                        putbinary((char *)mp->base + (n_comp*comp_size)*n,
454 >                                        comp_size, n_comp, stdout);
455                  }
456 +            }
457              if (ferror(stdout)) {
458                  fprintf(stderr, "Error writing to stdout\n");
459                  return(0);
# Line 360 | Line 463 | do_transpose(const MEMLOAD *mp)
463                  free_records(rp);
464          return(1);
465   badspec:
466 <        fprintf(stderr, "Bad transpose specification -- check dimension(s)\n");
466 >        fprintf(stderr, "Bad dimension(s)\n");
467          return(0);
468   }
469  
# Line 372 | Line 475 | do_resize(FILE *fp)
475          int     columns2go = no_columns;
476          char    word[256];
477                                                  /* sanity checks */
478 <        if (comp_size)
479 <                return(output_stream(fp));      /* binary data -- just copy */
478 >        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
479 >                return(output_stream(fp));      /* no-op -- just copy */
480          if (no_columns <= 0) {
481                  fprintf(stderr, "Missing -oc specification\n");
482                  return(0);
# Line 426 | Line 529 | done:
529   static int
530   headline(char *s, void *p)
531   {
532 <        static char     fmt[32];
532 >        static char     fmt[MAXFMTLEN];
533          int             n;
534  
535          if (formatval(fmt, s)) {
# Line 460 | Line 563 | headline(char *s, void *p)
563          if (!strncmp(s, "NCOMP=", 6)) {
564                  n = atoi(s+6);
565                  if ((n_comp > 0) & (n != n_comp)) {
566 <                        fputs("Incorrect number of components", stderr);
566 >                        fputs("Incorrect number of components\n", stderr);
567                          return(-1);
568                  }
569                  n_comp = n;
570                  return(0);
571          }
572 <        fputs(s, stdout);                       /* copy header info. */
572 >        if (o_header)
573 >                fputs(s, stdout);               /* copy header info. */
574          return(0);
575   }
576  
# Line 474 | Line 578 | headline(char *s, void *p)
578   int
579   main(int argc, char *argv[])
580   {
581 <        int     i_header = 1;                   /* input header? */
478 <        int     o_header = 1;                   /* output header? */
479 <        int     transpose = 0;                  /* transpose rows & cols? */
480 <        int     i;
581 >        int     a;
582  
583 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
584 <                switch (argv[i][1]) {
583 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
584 >                switch (argv[a][1]) {
585                  case 'i':                       /* input */
586 <                        if (argv[i][2] == 'c')  /* columns */
587 <                                ni_columns = atoi(argv[++i]);
588 <                        else if (argv[i][2] == 'r')
589 <                                ni_rows = atoi(argv[++i]);
586 >                        if (argv[a][2] == 'c')  /* columns */
587 >                                ni_columns = atoi(argv[++a]);
588 >                        else if (argv[a][2] == 'r')
589 >                                ni_rows = atoi(argv[++a]);
590                          else
591                                  goto userr;
592                          break;
593                  case 'o':                       /* output */
594 <                        if (argv[i][2] == 'c')  /* columns */
595 <                                no_columns = atoi(argv[++i]);
596 <                        else if (argv[i][2] == 'r')
597 <                                no_rows = atoi(argv[++i]);
598 <                        else
594 >                        if (argv[a][2] == 'c')  /* columns */
595 >                                no_columns = atoi(argv[++a]);
596 >                        else if (argv[a][2] == 'r')
597 >                                no_rows = atoi(argv[++a]);
598 >                        else if (argv[a][2] ||
599 >                          !(outLevels=get_array(argv[++a], outArray, MAXLEVELS)))
600                                  goto userr;
601                          break;
602                  case 'h':                       /* turn off header */
603 <                        switch (argv[i][2]) {
603 >                        switch (argv[a][2]) {
604                          case 'i':
605                                  i_header = 0;
606                                  break;
# Line 516 | Line 618 | main(int argc, char *argv[])
618                          transpose = !transpose;
619                          break;
620                  case 'f':                       /* format */
621 <                        switch (argv[i][2]) {
621 >                        switch (argv[a][2]) {
622                          case 'a':               /* ASCII */
623                          case 'A':
624                                  fmtid = "ascii";
# Line 540 | Line 642 | main(int argc, char *argv[])
642                          default:
643                                  goto userr;
644                          }
645 <                        if (argv[i][3]) {
646 <                                if (!isdigit(argv[i][3]))
645 >                        if (argv[a][3]) {
646 >                                if (!isdigit(argv[a][3]))
647                                          goto userr;
648 <                                n_comp = atoi(argv[i]+3);
649 <                        }
648 >                                n_comp = atoi(argv[a]+3);
649 >                        } else
650 >                                n_comp = 1;
651                          break;
652                  case 'w':                       /* warnings on/off */
653                          warnings = !warnings;
# Line 552 | Line 655 | main(int argc, char *argv[])
655                  default:
656                          goto userr;
657                  }
658 <        if (i < argc-1)                         /* arg count OK? */
658 >        if (a < argc-1)                         /* arg count OK? */
659                  goto userr;
660 +        if (outLevels) {                        /* should check consistency? */
661 +                no_rows = outArray[0][0];
662 +                no_columns = outArray[0][1];
663 +        }
664                                                  /* open input file? */
665 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
666 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
665 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
666 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
667                  return(1);
668          }
669          if (comp_size) {
# Line 564 | Line 671 | main(int argc, char *argv[])
671                  SET_FILE_BINARY(stdout);
672          }
673                                                  /* check for no-op */
674 <        if (!transpose & (i_header == o_header) && (comp_size ||
675 <                        (no_columns == ni_columns) & (no_rows == ni_rows))) {
674 >        if (!transpose & (outLevels <= 1) & (i_header == o_header) &&
675 >                        (no_columns == ni_columns) & (no_rows == ni_rows)) {
676                  if (warnings)
677                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
678                                  argv[0]);
# Line 574 | Line 681 | main(int argc, char *argv[])
681                  return(0);
682          }
683          if (i_header) {                         /* read header */
684 <                if (getheader(stdin, &headline, NULL) < 0)
684 >                if (getheader(stdin, headline, NULL) < 0)
685                          return(1);
686                  if (!check_sizes())
687                          return(1);
# Line 584 | Line 691 | main(int argc, char *argv[])
691                  }
692          } else if (!check_sizes())
693                  return(1);
694 <        if (o_header) {                         /* write header */
695 <                printargs(argc, argv, stdout);
696 <                if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
697 <                        if (ni_rows > 0) no_columns = ni_rows;
591 <                        if (ni_columns > 0) no_rows = ni_columns;
592 <                }
694 >        if (o_header) {                         /* write/add to header */
695 >                if (!i_header)
696 >                        newheader("RADIANCE", stdout);
697 >                printargs(a, argv, stdout);
698                  if (no_rows > 0)
699                          printf("NROWS=%d\n", no_rows);
700                  if (no_columns > 0)
# Line 598 | Line 703 | main(int argc, char *argv[])
703                  fputformat(fmtid, stdout);
704                  fputc('\n', stdout);            /* finish new header */
705          }
706 <        if (transpose) {                        /* transposing rows & columns? */
707 <                MEMLOAD myMem;                  /* need to load into memory */
708 <                if (i == argc-1) {
706 >        if (transpose | (outLevels > 1)) {      /* moving stuff around? */
707 >                MEMLOAD myMem;                  /* need to map into memory */
708 >                if (a == argc-1) {
709                          if (load_file(&myMem, stdin) <= 0) {
710                                  fprintf(stderr, "%s: error loading file into memory\n",
711 <                                                argv[i]);
711 >                                                argv[a]);
712                                  return(1);
713                          }
714                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 611 | Line 716 | main(int argc, char *argv[])
716                                                  argv[0]);
717                          return(1);
718                  }
719 <                if (!do_transpose(&myMem))
719 >                if (!do_reorder(&myMem))
720                          return(1);
721 <                /* free_load(&myMem); */
722 <        } else if (!do_resize(stdin))           /* just reshaping input */
721 >                /* free_load(&myMem);   about to exit, so don't bother */
722 >        } else if (!do_resize(stdin))           /* reshaping input */
723                  return(1);
724          return(0);
725   userr:
726          fprintf(stderr,
727 < "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
727 > "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",
728                          argv[0]);
729          return(1);
730   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines