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.7 by greg, Mon Nov 18 22:02:12 2013 UTC vs.
Revision 2.11 by greg, Fri May 30 17:29:28 2014 UTC

# Line 103 | 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 114 | 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 127 | 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 263 | 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 {
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 284 | 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 320 | 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 344 | 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",
349 <                                record_width);
375 >        if (comp_size) {
376 >                fputs("Bad call to do_resize (binary input)\n", stderr);
377                  return(0);
378          }
379          if (no_columns <= 0) {
# Line 364 | Line 391 | do_resize(FILE *fp)
391          do {                                    /* reshape records */
392                  int     n;
393  
394 <                for (n = record_width; n--; ) {
394 >                for (n = n_comp; n--; ) {
395                          if (fget_word(word, fp) == NULL) {
396 <                                if (records2go > 0 || n < record_width-1)
396 >                                if (records2go > 0 || n < n_comp-1)
397                                          break;
398                                  goto done;      /* normal EOD */
399                          }
# Line 401 | Line 428 | done:
428   static int
429   headline(char *s, void *p)
430   {
431 <        char    fmt[32];
431 >        static char     fmt[32];
432 >        int             n;
433  
434          if (formatval(fmt, s)) {
435 +                if (fmtid == NULL) {
436 +                        fmtid = fmt;
437 +                        return(0);
438 +                }
439                  if (!strcmp(fmt, fmtid))
440                          return(0);
441                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
442                  return(-1);
443          }
444 +        if (!strncmp(s, "NROWS=", 6)) {
445 +                n = atoi(s+6);
446 +                if ((ni_rows > 0) & (n != ni_rows)) {
447 +                        fputs("Incorrect input row count\n", stderr);
448 +                        return(-1);
449 +                }
450 +                ni_rows = n;
451 +                return(0);
452 +        }
453 +        if (!strncmp(s, "NCOLS=", 6)) {
454 +                n = atoi(s+6);
455 +                if ((ni_columns > 0) & (n != ni_columns)) {
456 +                        fputs("Incorrect input column count\n", stderr);
457 +                        return(-1);
458 +                }
459 +                ni_columns = n;
460 +                return(0);
461 +        }
462 +        if (!strncmp(s, "NCOMP=", 6)) {
463 +                n = atoi(s+6);
464 +                if ((n_comp > 0) & (n != n_comp)) {
465 +                        fputs("Incorrect number of components", stderr);
466 +                        return(-1);
467 +                }
468 +                n_comp = n;
469 +                return(0);
470 +        }
471          fputs(s, stdout);                       /* copy header info. */
472          return(0);
473   }
# Line 417 | Line 476 | headline(char *s, void *p)
476   int
477   main(int argc, char *argv[])
478   {
479 <        int     do_header = 1;                  /* header i/o? */
479 >        int     i_header = 1;                   /* input header? */
480 >        int     o_header = 1;                   /* output header? */
481          int     transpose = 0;                  /* transpose rows & cols? */
482          int     i;
483  
# Line 439 | Line 499 | main(int argc, char *argv[])
499                          else
500                                  goto userr;
501                          break;
502 <                case 'h':                       /* header on/off */
503 <                        do_header = !do_header;
502 >                case 'h':                       /* turn off header */
503 >                        switch (argv[i][2]) {
504 >                        case 'i':
505 >                                i_header = 0;
506 >                                break;
507 >                        case 'o':
508 >                                o_header = 0;
509 >                                break;
510 >                        case '\0':
511 >                                i_header = o_header = 0;
512 >                                break;
513 >                        default:
514 >                                goto userr;
515 >                        }
516                          break;
517                  case 't':                       /* transpose on/off */
518                          transpose = !transpose;
# Line 450 | Line 522 | main(int argc, char *argv[])
522                          case 'a':               /* ASCII */
523                          case 'A':
524                                  fmtid = "ascii";
525 <                                record_width = 1;
525 >                                comp_size = 0;
526                                  break;
527                          case 'f':               /* float */
528                          case 'F':
529                                  fmtid = "float";
530 <                                record_width = -(int)sizeof(float);
530 >                                comp_size = sizeof(float);
531                                  break;
532                          case 'd':               /* double */
533                          case 'D':
534                                  fmtid = "double";
535 <                                record_width = -(int)sizeof(double);
535 >                                comp_size = sizeof(double);
536                                  break;
537                          case 'b':               /* binary (bytes) */
538                          case 'B':
539                                  fmtid = "byte";
540 <                                record_width = -1;
540 >                                comp_size = 1;
541                                  break;
542                          default:
543                                  goto userr;
# Line 473 | Line 545 | main(int argc, char *argv[])
545                          if (argv[i][3]) {
546                                  if (!isdigit(argv[i][3]))
547                                          goto userr;
548 <                                record_width *= atoi(argv[i]+3);
548 >                                n_comp = atoi(argv[i]+3);
549                          }
550                          break;
551                  case 'w':                       /* warnings on/off */
# Line 482 | Line 554 | main(int argc, char *argv[])
554                  default:
555                          goto userr;
556                  }
485        if (!record_width)
486                goto userr;
557          if (i < argc-1)                         /* arg count OK? */
558                  goto userr;
559                                                  /* open input file? */
# Line 491 | Line 561 | main(int argc, char *argv[])
561                  fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
562                  return(1);
563          }
564 <        if (record_width < 0) {
564 >        if (comp_size) {
565                  SET_FILE_BINARY(stdin);
566                  SET_FILE_BINARY(stdout);
567          }
568                                                  /* check for no-op */
569 <        if (!transpose && (record_width < 0 ||
569 >        if (!transpose && (comp_size ||
570                          (no_columns == ni_columns) & (no_rows == ni_rows))) {
571                  if (warnings)
572                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
# Line 505 | Line 575 | main(int argc, char *argv[])
575                          return(1);
576                  return(0);
577          }
578 <        if (do_header) {                        /* read/write header */
578 >        if (i_header) {                         /* read header */
579                  if (getheader(stdin, &headline, NULL) < 0)
580                          return(1);
581 +                if (!check_sizes())
582 +                        return(1);
583 +                if (comp_size) {                /* a little late... */
584 +                        SET_FILE_BINARY(stdin);
585 +                        SET_FILE_BINARY(stdout);
586 +                }
587 +        } else if (!check_sizes())
588 +                return(1);
589 +        if (o_header) {                         /* write header */
590                  printargs(argc, argv, stdout);
591 +                if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
592 +                        if (ni_rows > 0) no_columns = ni_rows;
593 +                        if (ni_columns > 0) no_rows = ni_columns;
594 +                }
595 +                if (no_rows > 0)
596 +                        printf("NROWS=%d\n", no_rows);
597 +                if (no_columns > 0)
598 +                        printf("NCOLS=%d\n", no_columns);
599 +                printf("NCOMP=%d\n", n_comp);
600                  fputformat(fmtid, stdout);
601                  fputc('\n', stdout);            /* finish new header */
602          }
# Line 533 | Line 621 | main(int argc, char *argv[])
621          return(0);
622   userr:
623          fprintf(stderr,
624 < "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
624 > "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
625                          argv[0]);
626          return(1);
627   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines