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.6 by greg, Mon Nov 18 18:07:16 2013 UTC vs.
Revision 2.14 by greg, Sat May 31 19:21:21 2014 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include "platform.h"
12   #include "rtio.h"
13   #include "resolu.h"
14 < #ifndef _WIN32
14 > #ifdef _WIN32
15 > #undef ftello
16 > #define ftello  ftell
17 > #undef ssize_t
18 > #define ssize_t size_t
19 > #else
20   #include <sys/mman.h>
21   #endif
22  
# Line 98 | Line 103 | load_file(MEMLOAD *mp, FILE *fp)
103   static int
104   load_stream(MEMLOAD *mp, FILE *fp)
105   {
106 +        size_t  alloced = 0;
107          char    buf[8192];
108          size_t  nr;
109  
# Line 109 | Line 115 | load_stream(MEMLOAD *mp, FILE *fp)
115          if (fp == NULL)
116                  return(-1);
117          while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
118 <                if (!mp->len)
118 >                if (!alloced)
119                          mp->base = malloc(nr);
120 <                else
121 <                        mp->base = realloc(mp->base, mp->len+nr);
120 >                else if (mp->len+nr > alloced)
121 >                        mp->base = realloc(mp->base,
122 >                                alloced = alloced*(2+(nr==sizeof(buf)))/2+nr);
123                  if (mp->base == NULL)
124                          return(-1);
125                  memcpy((char *)mp->base + mp->len, buf, nr);
# Line 122 | Line 129 | load_stream(MEMLOAD *mp, FILE *fp)
129                  free_load(mp);
130                  return(-1);
131          }
132 +        if (alloced > mp->len*5/4)      /* don't waste too much space */
133 +                mp->base = realloc(mp->base, mp->len);
134          return(mp->len > 0);
135   }
136  
# Line 228 | Line 237 | output_stream(FILE *fp)
237  
238          if (fp == NULL)
239                  return(0);
240 <        fflush(stdout);                 /* assumes nothing in input buffer */
241 <        while ((n = read(fileno(fp), buf, sizeof(buf))) > 0)
240 >        fflush(stdout);
241 >        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
242                  if (write(fileno(stdout), buf, n) != n)
243                          return(0);
244 <        return(n >= 0);
244 >        return(!ferror(fp));
245   }
246  
247   /* get next word from stream, leaving stream on EOL or start of next word */
# Line 258 | Line 267 | fget_word(char buf[256], FILE *fp)
267          return(buf);
268   }
269  
270 < char            *fmtid = "ascii";               /* format id */
271 < int             record_width = 3;               /* words/record (<0 binary) */
270 > char            *fmtid = NULL;                  /* format id */
271 > int             comp_size = 0;                  /* binary bytes/channel */
272 > int             n_comp = 0;                     /* components/record */
273   int             ni_columns = 0;                 /* number of input columns */
274   int             ni_rows = 0;                    /* number of input rows */
275   int             no_columns = 0;                 /* number of output columns */
276   int             no_rows = 0;                    /* number of output rows */
277  
278 + /* check settings and assign defaults */
279 + static int
280 + check_sizes()
281 + {
282 +        if (fmtid == NULL) {
283 +                fmtid = "ascii";
284 +        } else if (!comp_size) {
285 +                if (!strcmp(fmtid, "float"))
286 +                        comp_size = sizeof(float);
287 +                else if (!strcmp(fmtid, "double"))
288 +                        comp_size = sizeof(double);
289 +                else if (!strcmp(fmtid, "byte"))
290 +                        comp_size = 1;
291 +                else if (strcmp(fmtid, "ascii")) {
292 +                        fprintf(stderr, "Unsupported format: %s\n", fmtid);
293 +                        return(0);
294 +                }
295 +        }
296 +        if (n_comp <= 0)
297 +                n_comp = 3;
298 +        return(1);
299 + }
300 +
301   /* output transposed ASCII or binary data from memory */
302   static int
303   do_transpose(const MEMLOAD *mp)
# Line 279 | Line 312 | do_transpose(const MEMLOAD *mp)
312          if (ni_columns <= 0)
313                  ni_columns = no_rows;
314                                                  /* get # records (& index) */
315 <        if (record_width > 0) {
316 <                if ((rp = index_records(mp, record_width)) == NULL)
315 >        if (!comp_size) {
316 >                if ((rp = index_records(mp, n_comp)) == NULL)
317                          return(0);
318                  if (ni_columns <= 0)
319                          ni_columns = count_columns(rp);
320                  nrecords = rp->nrecs;
321          } else if ((ni_rows > 0) & (ni_columns > 0)) {
322                  nrecords = ni_rows*ni_columns;
323 <                if (nrecords > mp->len / -record_width) {
323 >                if (nrecords > mp->len/(n_comp*comp_size)) {
324                          fprintf(stderr,
325                              "Input too small for specified size and type\n");
326                          return(0);
327                  }
328          } else
329 <                nrecords = mp->len / -record_width;
329 >                nrecords = mp->len/(n_comp*comp_size);
330                                                  /* check sizes */
331          if ((ni_rows <= 0) & (ni_columns > 0))
332                  ni_rows = nrecords/ni_columns;
# Line 315 | Line 348 | do_transpose(const MEMLOAD *mp)
348                          putc(tabEOL[j >= no_columns-1], stdout);
349                  } else {                        /* binary output */
350                          fwrite((char *)mp->base +
351 <                                        -record_width*(j*ni_columns + i),
352 <                                        -record_width, 1, stdout);
351 >                                        (n_comp*comp_size)*(j*ni_columns + i),
352 >                                        n_comp*comp_size, 1, stdout);
353                  }
354              if (ferror(stdout)) {
355                  fprintf(stderr, "Error writing to stdout\n");
# Line 339 | Line 372 | do_resize(FILE *fp)
372          int     columns2go = no_columns;
373          char    word[256];
374                                                  /* sanity checks */
375 <        if (record_width <= 0) {
376 <                fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
344 <                                record_width);
345 <                return(0);
346 <        }
375 >        if (comp_size)
376 >                return(output_stream(fp));      /* binary data -- just copy */
377          if (no_columns <= 0) {
378                  fprintf(stderr, "Missing -oc specification\n");
379                  return(0);
# Line 359 | Line 389 | do_resize(FILE *fp)
389          do {                                    /* reshape records */
390                  int     n;
391  
392 <                for (n = record_width; n--; ) {
392 >                for (n = n_comp; n--; ) {
393                          if (fget_word(word, fp) == NULL) {
394 <                                if (records2go > 0 || n < record_width-1)
394 >                                if (records2go > 0 || n < n_comp-1)
395                                          break;
396                                  goto done;      /* normal EOD */
397                          }
# Line 396 | Line 426 | done:
426   static int
427   headline(char *s, void *p)
428   {
429 <        char    fmt[32];
429 >        static char     fmt[32];
430 >        int             n;
431  
432          if (formatval(fmt, s)) {
433 +                if (fmtid == NULL) {
434 +                        fmtid = fmt;
435 +                        return(0);
436 +                }
437                  if (!strcmp(fmt, fmtid))
438                          return(0);
439                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
440                  return(-1);
441          }
442 +        if (!strncmp(s, "NROWS=", 6)) {
443 +                n = atoi(s+6);
444 +                if ((ni_rows > 0) & (n != ni_rows)) {
445 +                        fputs("Incorrect input row count\n", stderr);
446 +                        return(-1);
447 +                }
448 +                ni_rows = n;
449 +                return(0);
450 +        }
451 +        if (!strncmp(s, "NCOLS=", 6)) {
452 +                n = atoi(s+6);
453 +                if ((ni_columns > 0) & (n != ni_columns)) {
454 +                        fputs("Incorrect input column count\n", stderr);
455 +                        return(-1);
456 +                }
457 +                ni_columns = n;
458 +                return(0);
459 +        }
460 +        if (!strncmp(s, "NCOMP=", 6)) {
461 +                n = atoi(s+6);
462 +                if ((n_comp > 0) & (n != n_comp)) {
463 +                        fputs("Incorrect number of components\n", stderr);
464 +                        return(-1);
465 +                }
466 +                n_comp = n;
467 +                return(0);
468 +        }
469          fputs(s, stdout);                       /* copy header info. */
470          return(0);
471   }
# Line 412 | Line 474 | headline(char *s, void *p)
474   int
475   main(int argc, char *argv[])
476   {
477 <        int     do_header = 1;                  /* header i/o? */
477 >        int     i_header = 1;                   /* input header? */
478 >        int     o_header = 1;                   /* output header? */
479          int     transpose = 0;                  /* transpose rows & cols? */
480 <        int     i;
480 >        int     a;
481  
482 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
483 <                switch (argv[i][1]) {
482 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
483 >                switch (argv[a][1]) {
484                  case 'i':                       /* input */
485 <                        if (argv[i][2] == 'c')  /* columns */
486 <                                ni_columns = atoi(argv[++i]);
487 <                        else if (argv[i][2] == 'r')
488 <                                ni_rows = atoi(argv[++i]);
485 >                        if (argv[a][2] == 'c')  /* columns */
486 >                                ni_columns = atoi(argv[++a]);
487 >                        else if (argv[a][2] == 'r')
488 >                                ni_rows = atoi(argv[++a]);
489                          else
490                                  goto userr;
491                          break;
492                  case 'o':                       /* output */
493 <                        if (argv[i][2] == 'c')  /* columns */
494 <                                no_columns = atoi(argv[++i]);
495 <                        else if (argv[i][2] == 'r')
496 <                                no_rows = atoi(argv[++i]);
493 >                        if (argv[a][2] == 'c')  /* columns */
494 >                                no_columns = atoi(argv[++a]);
495 >                        else if (argv[a][2] == 'r')
496 >                                no_rows = atoi(argv[++a]);
497                          else
498                                  goto userr;
499                          break;
500 <                case 'h':                       /* header on/off */
501 <                        do_header = !do_header;
500 >                case 'h':                       /* turn off header */
501 >                        switch (argv[a][2]) {
502 >                        case 'i':
503 >                                i_header = 0;
504 >                                break;
505 >                        case 'o':
506 >                                o_header = 0;
507 >                                break;
508 >                        case '\0':
509 >                                i_header = o_header = 0;
510 >                                break;
511 >                        default:
512 >                                goto userr;
513 >                        }
514                          break;
515                  case 't':                       /* transpose on/off */
516                          transpose = !transpose;
517                          break;
518                  case 'f':                       /* format */
519 <                        switch (argv[i][2]) {
519 >                        switch (argv[a][2]) {
520                          case 'a':               /* ASCII */
521                          case 'A':
522                                  fmtid = "ascii";
523 <                                record_width = 1;
523 >                                comp_size = 0;
524                                  break;
525                          case 'f':               /* float */
526                          case 'F':
527                                  fmtid = "float";
528 <                                record_width = -(int)sizeof(float);
528 >                                comp_size = sizeof(float);
529                                  break;
530                          case 'd':               /* double */
531                          case 'D':
532                                  fmtid = "double";
533 <                                record_width = -(int)sizeof(double);
533 >                                comp_size = sizeof(double);
534                                  break;
535                          case 'b':               /* binary (bytes) */
536                          case 'B':
537                                  fmtid = "byte";
538 <                                record_width = -1;
538 >                                comp_size = 1;
539                                  break;
540                          default:
541                                  goto userr;
542                          }
543 <                        if (argv[i][3]) {
544 <                                if (!isdigit(argv[i][3]))
543 >                        if (argv[a][3]) {
544 >                                if (!isdigit(argv[a][3]))
545                                          goto userr;
546 <                                record_width *= atoi(argv[i]+3);
547 <                        }
546 >                                n_comp = atoi(argv[a]+3);
547 >                        } else
548 >                                n_comp = 1;
549                          break;
550                  case 'w':                       /* warnings on/off */
551                          warnings = !warnings;
# Line 477 | Line 553 | main(int argc, char *argv[])
553                  default:
554                          goto userr;
555                  }
556 <        if (!record_width)
556 >        if (a < argc-1)                         /* arg count OK? */
557                  goto userr;
482        if (i < argc-1)                         /* arg count OK? */
483                goto userr;
558                                                  /* open input file? */
559 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
560 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
559 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
560 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
561                  return(1);
562          }
563 <        if (record_width < 0) {
563 >        if (comp_size) {
564                  SET_FILE_BINARY(stdin);
565                  SET_FILE_BINARY(stdout);
566          }
567                                                  /* check for no-op */
568 <        if (!transpose && (record_width < 0 ||
568 >        if (!transpose & (i_header == o_header) && (comp_size ||
569                          (no_columns == ni_columns) & (no_rows == ni_rows))) {
570                  if (warnings)
571                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
# Line 500 | Line 574 | main(int argc, char *argv[])
574                          return(1);
575                  return(0);
576          }
577 <        if (do_header) {                        /* read/write header */
577 >        if (i_header) {                         /* read header */
578                  if (getheader(stdin, &headline, NULL) < 0)
579                          return(1);
580 <                printargs(argc, argv, stdout);
580 >                if (!check_sizes())
581 >                        return(1);
582 >                if (comp_size) {                /* a little late... */
583 >                        SET_FILE_BINARY(stdin);
584 >                        SET_FILE_BINARY(stdout);
585 >                }
586 >        } else if (!check_sizes())
587 >                return(1);
588 >        if (o_header) {                         /* write header */
589 >                printargs(a, argv, stdout);
590 >                if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
591 >                        if (ni_rows > 0) no_columns = ni_rows;
592 >                        if (ni_columns > 0) no_rows = ni_columns;
593 >                }
594 >                if (no_rows > 0)
595 >                        printf("NROWS=%d\n", no_rows);
596 >                if (no_columns > 0)
597 >                        printf("NCOLS=%d\n", no_columns);
598 >                printf("NCOMP=%d\n", n_comp);
599                  fputformat(fmtid, stdout);
600                  fputc('\n', stdout);            /* finish new header */
601          }
602          if (transpose) {                        /* transposing rows & columns? */
603                  MEMLOAD myMem;                  /* need to load into memory */
604 <                if (i == argc-1) {
604 >                if (a == argc-1) {
605                          if (load_file(&myMem, stdin) <= 0) {
606                                  fprintf(stderr, "%s: error loading file into memory\n",
607 <                                                argv[i]);
607 >                                                argv[a]);
608                                  return(1);
609                          }
610                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 528 | Line 620 | main(int argc, char *argv[])
620          return(0);
621   userr:
622          fprintf(stderr,
623 < "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
623 > "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
624                          argv[0]);
625          return(1);
626   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines