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.37 by greg, Fri Jan 15 17:22:23 2021 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include <stdlib.h>
9 #include <string.h>
9   #include <ctype.h>
10   #include "platform.h"
11   #include "rtio.h"
# Line 20 | Line 19 | static const char RCSid[] = "$Id$";
19    #include <sys/mman.h>
20   #endif
21  
22 + #define MAXLEVELS       16      /* max RxC.. block pairs */
23 +
24   typedef struct {
25 +        void    *mapped;        /* memory-mapped pointer */
26          void    *base;          /* pointer to base memory */
27          size_t  len;            /* allocated memory length */
26        int     mapped;         /* memory-mapped file? */
28   } MEMLOAD;              /* file loaded/mapped into memory */
29  
30   typedef struct {
31          int     nw_rec;         /* number of words per record */
32 <        int     nrecs;          /* number of records we found */
32 >        ssize_t nrecs;          /* number of records we found */
33          char    *rec[1];        /* record array (extends struct) */
34   } RECINDEX;
35  
# Line 42 | 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   }
# Line 60 | Line 62 | load_stream(MEMLOAD *mp, FILE *fp)
62  
63          if (mp == NULL)
64                  return(-1);
65 +        mp->mapped = NULL;
66          mp->base = NULL;
67          mp->len = 0;
65        mp->mapped = 0;
68          if (fp == NULL)
69                  return(-1);
70          while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
# Line 90 | Line 92 | static int
92   load_file(MEMLOAD *mp, FILE *fp)
93   {
94          int     fd;
95 <        off_t   skip, flen, skipped;
95 >        off_t   skip, flen, fpos;
96  
97   #if defined(_WIN32) || defined(_WIN64)
98                                  /* too difficult to fix this */
# Line 98 | Line 100 | load_file(MEMLOAD *mp, FILE *fp)
100   #endif
101          if (mp == NULL)
102                  return(-1);
103 +        mp->mapped = NULL;
104          mp->base = NULL;
105          mp->len = 0;
103        mp->mapped = 0;
106          if (fp == NULL)
107                  return(-1);
108          fd = fileno(fp);
# Line 111 | Line 113 | load_file(MEMLOAD *mp, FILE *fp)
113          mp->len = (size_t)(flen - skip);
114   #ifdef MAP_FILE
115          if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
116 <                mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
117 <                if (mp->base != MAP_FAILED) {
118 <                        mp->base = (char *)mp->base + skip;
117 <                        mp->mapped = 1;
116 >                mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
117 >                if (mp->mapped != MAP_FAILED) {
118 >                        mp->base = (char *)mp->mapped + skip;
119                          return(1);      /* mmap() success */
120                  }
121 <                mp->base = NULL;        /* fall back to reading it in... */
121 >                mp->mapped = NULL;      /* else fall back to reading it in... */
122          }
123   #endif
124          if (lseek(fd, skip, SEEK_SET) != skip ||
# Line 125 | Line 126 | load_file(MEMLOAD *mp, FILE *fp)
126                  mp->len = 0;
127                  return(-1);
128          }
129 <        skipped = skip;                 /* read() fails on really big buffers */
130 <        while (skipped < flen) {
131 <                ssize_t nread = read(fd, (char *)mp->base+(skipped-skip),
132 <                                (flen-skipped <= 1L<<30) ? flen-skipped : 1L<<30);
129 >        fpos = skip;
130 >        while (fpos < flen) {           /* read() fails if n > 2 GBytes */
131 >                ssize_t nread = read(fd, (char *)mp->base+(fpos-skip),
132 >                                (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24);
133                  if (nread <= 0) {
134                          free_load(mp);
135                          return(-1);
136                  }
137 <                skipped += nread;
137 >                fpos += nread;
138          }
139          return(1);
140   }
# Line 217 | 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 290 | Line 291 | 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 + /* 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 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_transpose(const MEMLOAD *mp)
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 = no_columns;
411 >                ni_rows = transpose ? no_columns : no_rows;
412          if (ni_columns <= 0)
413 <                ni_columns = no_rows;
413 >                ni_columns = transpose ? no_rows : no_columns;
414                                                  /* get # records (& index) */
415          if (!comp_size) {
416                  if ((rp = index_records(mp, n_comp)) == NULL)
# Line 341 | Line 419 | do_transpose(const MEMLOAD *mp)
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;
422 >                nrecords = (ssize_t)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 352 | Line 430 | do_transpose(const MEMLOAD *mp)
430                                                  /* check sizes */
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 >                fputc('\n', stdout);
460 >        }
461 >                                                /* reorder records */
462          for (i = 0; i < no_rows; i++) {
463 <            for (j = 0; j < no_columns; j++)
463 >            for (j = 0; j < no_columns; j++) {
464 >                ssize_t n = get_input_pos(i, j);
465 >                if (n >= nrecords) {
466 >                        fputs("Index past end-of-file\n", stderr);
467 >                        return(0);
468 >                }
469                  if (rp != NULL) {               /* ASCII output */
470 <                        print_record(rp, j*ni_columns + i);
470 >                        print_record(rp, n);
471                          putc(tabEOL[j >= no_columns-1], stdout);
472                  } else {                        /* binary output */
473 <                        putbinary((char *)mp->base +
373 <                            (unsigned long)(n_comp*comp_size)*(j*ni_columns + i),
473 >                        putbinary((char *)mp->base + (n_comp*comp_size)*n,
474                                          comp_size, n_comp, stdout);
475                  }
476 +            }
477              if (ferror(stdout)) {
478                  fprintf(stderr, "Error writing to stdout\n");
479                  return(0);
# Line 382 | Line 483 | do_transpose(const MEMLOAD *mp)
483                  free_records(rp);
484          return(1);
485   badspec:
486 <        fprintf(stderr, "Bad transpose specification -- check dimension(s)\n");
486 >        fprintf(stderr, "Bad dimension(s)\n");
487          return(0);
488   }
489  
# Line 390 | Line 491 | badspec:
491   static int
492   do_resize(FILE *fp)
493   {
494 <        long    records2go = ni_rows*ni_columns;
494 >        ssize_t records2go = ni_rows*ni_columns;
495          int     columns2go = no_columns;
496          char    word[256];
497 +                        
498 +        if (o_header) {                         /* finish header? */
499 +                if (no_rows > 0)
500 +                        printf("NROWS=%d\n", no_rows);
501 +                if (no_columns > 0)
502 +                        printf("NCOLS=%d\n", no_columns);
503 +                fputc('\n', stdout);
504 +        }
505                                                  /* sanity checks */
506 <        if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows))
506 >        if (comp_size || !check &
507 >                        (no_columns == ni_columns) & (no_rows == ni_rows))
508                  return(output_stream(fp));      /* no-op -- just copy */
509          if (no_columns <= 0) {
510                  fprintf(stderr, "Missing -oc specification\n");
# Line 404 | Line 514 | do_resize(FILE *fp)
514                  records2go = no_rows*no_columns;
515          else if (no_rows*no_columns != records2go) {
516                  fprintf(stderr,
517 <                        "Input and output data sizes disagree (%dx%d != %dx%d)\n",
517 >                        "Number of input and output records disagree (%dx%d != %dx%d)\n",
518                                  ni_rows, ni_columns, no_rows, no_columns);
519                  return(0);
520          }
# Line 514 | Line 624 | main(int argc, char *argv[])
624                                  no_columns = atoi(argv[++a]);
625                          else if (argv[a][2] == 'r')
626                                  no_rows = atoi(argv[++a]);
627 <                        else
627 >                        else if (argv[a][2] ||
628 >                          !(outLevels=get_array(argv[++a], outArray, MAXLEVELS)))
629                                  goto userr;
630                          break;
631                  case 'h':                       /* turn off header */
# Line 570 | Line 681 | main(int argc, char *argv[])
681                  case 'w':                       /* warnings on/off */
682                          warnings = !warnings;
683                          break;
684 +                case 'c':                       /* force check operation */
685 +                        check = 1;
686 +                        break;
687                  default:
688                          goto userr;
689                  }
690          if (a < argc-1)                         /* arg count OK? */
691                  goto userr;
692 +        if (outLevels) {                        /* should check consistency? */
693 +                no_rows = outArray[0][0];
694 +                no_columns = outArray[0][1];
695 +        }
696                                                  /* open input file? */
697          if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
698                  fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
# Line 585 | Line 703 | main(int argc, char *argv[])
703                  SET_FILE_BINARY(stdout);
704          }
705                                                  /* check for no-op */
706 <        if (!transpose & (i_header == o_header) &&
706 >        if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) &&
707                          (no_columns == ni_columns) & (no_rows == ni_rows)) {
708                  if (warnings)
709                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
# Line 605 | Line 723 | main(int argc, char *argv[])
723                  }
724          } else if (!check_sizes())
725                  return(1);
726 <        if (o_header) {                         /* write header */
726 >        if (o_header) {                         /* write/add to header */
727 >                if (!i_header)
728 >                        newheader("RADIANCE", stdout);
729                  printargs(a, argv, stdout);
610                if (no_rows > 0)
611                        printf("NROWS=%d\n", no_rows);
612                if (no_columns > 0)
613                        printf("NCOLS=%d\n", no_columns);
730                  printf("NCOMP=%d\n", n_comp);
731                  fputformat(fmtid, stdout);
616                fputc('\n', stdout);            /* finish new header */
732          }
733 <        if (transpose) {                        /* transposing rows & columns? */
733 >        if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) {
734                  MEMLOAD myMem;                  /* need to map into memory */
735                  if (a == argc-1) {
736                          if (load_file(&myMem, stdin) <= 0) {
# Line 628 | Line 743 | main(int argc, char *argv[])
743                                                  argv[0]);
744                          return(1);
745                  }
746 <                if (!do_transpose(&myMem))
746 >                if (!do_reorder(&myMem))
747                          return(1);
748                  /* free_load(&myMem);   about to exit, so don't bother */
749 <        } else if (!do_resize(stdin))           /* reshaping input */
749 >        } else if (!do_resize(stdin))           /* just reshaping input */
750                  return(1);
751          return(0);
752   userr:
753          fprintf(stderr,
754 < "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
754 > "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",
755                          argv[0]);
756          return(1);
757   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines