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.27 by greg, Thu Oct 18 22:59:48 2018 UTC vs.
Revision 2.33 by greg, Fri Nov 8 16:36:36 2019 UTC

# Line 20 | Line 20 | static const char RCSid[] = "$Id$";
20    #include <sys/mman.h>
21   #endif
22  
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 */
26        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 42 | 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   }
# Line 60 | 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;
65        mp->mapped = 0;
69          if (fp == NULL)
70                  return(-1);
71          while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
# Line 90 | Line 93 | static int
93   load_file(MEMLOAD *mp, FILE *fp)
94   {
95          int     fd;
96 <        off_t   skip, flen, skipped;
96 >        off_t   skip, flen, fpos;
97  
98   #if defined(_WIN32) || defined(_WIN64)
99                                  /* too difficult to fix this */
# Line 98 | Line 101 | load_file(MEMLOAD *mp, FILE *fp)
101   #endif
102          if (mp == NULL)
103                  return(-1);
104 +        mp->mapped = NULL;
105          mp->base = NULL;
106          mp->len = 0;
103        mp->mapped = 0;
107          if (fp == NULL)
108                  return(-1);
109          fd = fileno(fp);
# Line 111 | Line 114 | load_file(MEMLOAD *mp, FILE *fp)
114          mp->len = (size_t)(flen - skip);
115   #ifdef MAP_FILE
116          if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
117 <                mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
118 <                if (mp->base != MAP_FAILED) {
119 <                        mp->base = (char *)mp->base + skip;
117 <                        mp->mapped = 1;
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->base = NULL;        /* fall back to reading it in... */
122 >                mp->mapped = NULL;      /* else fall back to reading it in... */
123          }
124   #endif
125          if (lseek(fd, skip, SEEK_SET) != skip ||
# Line 125 | Line 127 | load_file(MEMLOAD *mp, FILE *fp)
127                  mp->len = 0;
128                  return(-1);
129          }
130 <        skipped = skip;                 /* read() fails on really big buffers */
131 <        while (skipped < flen) {
132 <                ssize_t nread = read(fd, (char *)mp->base+(skipped-skip),
133 <                                (flen-skipped <= 1L<<30) ? flen-skipped : 1L<<30);
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 <                skipped += nread;
138 >                fpos += nread;
139          }
140          return(1);
141   }
# Line 217 | 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 290 | Line 292 | 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 320 | Line 359 | check_sizes()
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 343 | Line 421 | do_transpose(const MEMLOAD *mp)
421          } else if ((ni_rows > 0) & (ni_columns > 0)) {
422                  nrecords = ni_rows*ni_columns;
423                  if (nrecords > mp->len/(n_comp*comp_size)) {
424 <                        fprintf(stderr,
425 <                            "Input too small for specified size and type\n");
424 >                        fputs("Input too small for specified size and type\n",
425 >                                        stderr);
426                          return(0);
427                  }
428          } else
# Line 356 | 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 >        } 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 <                            (unsigned long)(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);
# Line 382 | Line 477 | do_transpose(const MEMLOAD *mp)
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  
# Line 514 | Line 609 | main(int argc, char *argv[])
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 */
# Line 575 | Line 671 | main(int argc, char *argv[])
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]);
# Line 585 | Line 685 | main(int argc, char *argv[])
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",
# Line 605 | Line 705 | main(int argc, char *argv[])
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);
# Line 615 | Line 717 | main(int argc, char *argv[])
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) {
# Line 628 | Line 730 | main(int argc, char *argv[])
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 */
# Line 636 | Line 738 | main(int argc, char *argv[])
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines