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.8 by greg, Fri Feb 21 13:49:00 2014 UTC vs.
Revision 2.16 by greg, Wed Jul 9 21:45:48 2014 UTC

# Line 237 | 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 267 | 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 + int             transpose = 0;                  /* transpose rows & cols? */
278 + int             i_header = 1;                   /* input header? */
279 + int             o_header = 1;                   /* output header? */
280  
281 + /* check settings and assign defaults */
282 + static int
283 + check_sizes()
284 + {
285 +        if (fmtid == NULL) {
286 +                fmtid = "ascii";
287 +        } else if (!comp_size) {
288 +                if (!strcmp(fmtid, "float"))
289 +                        comp_size = sizeof(float);
290 +                else if (!strcmp(fmtid, "double"))
291 +                        comp_size = sizeof(double);
292 +                else if (!strcmp(fmtid, "byte"))
293 +                        comp_size = 1;
294 +                else if (strcmp(fmtid, "ascii")) {
295 +                        fprintf(stderr, "Unsupported format: %s\n", fmtid);
296 +                        return(0);
297 +                }
298 +        }
299 +        if (transpose && (no_rows <= 0) & (no_columns <= 0)) {
300 +                if (ni_rows > 0) no_columns = ni_rows;
301 +                if (ni_columns > 0) no_rows = ni_columns;
302 +        } else if ((no_rows <= 0) & (no_columns > 0) &&
303 +                        !((ni_rows*ni_columns) % no_columns))
304 +                no_rows = ni_rows*ni_columns/no_columns;
305 +        if (n_comp <= 0)
306 +                n_comp = 3;
307 +        return(1);
308 + }
309 +
310   /* output transposed ASCII or binary data from memory */
311   static int
312   do_transpose(const MEMLOAD *mp)
# Line 288 | Line 321 | do_transpose(const MEMLOAD *mp)
321          if (ni_columns <= 0)
322                  ni_columns = no_rows;
323                                                  /* get # records (& index) */
324 <        if (record_width > 0) {
325 <                if ((rp = index_records(mp, record_width)) == NULL)
324 >        if (!comp_size) {
325 >                if ((rp = index_records(mp, n_comp)) == NULL)
326                          return(0);
327                  if (ni_columns <= 0)
328                          ni_columns = count_columns(rp);
329                  nrecords = rp->nrecs;
330          } else if ((ni_rows > 0) & (ni_columns > 0)) {
331                  nrecords = ni_rows*ni_columns;
332 <                if (nrecords > mp->len / -record_width) {
332 >                if (nrecords > mp->len/(n_comp*comp_size)) {
333                          fprintf(stderr,
334                              "Input too small for specified size and type\n");
335                          return(0);
336                  }
337          } else
338 <                nrecords = mp->len / -record_width;
338 >                nrecords = mp->len/(n_comp*comp_size);
339                                                  /* check sizes */
340          if ((ni_rows <= 0) & (ni_columns > 0))
341                  ni_rows = nrecords/ni_columns;
# Line 324 | Line 357 | do_transpose(const MEMLOAD *mp)
357                          putc(tabEOL[j >= no_columns-1], stdout);
358                  } else {                        /* binary output */
359                          fwrite((char *)mp->base +
360 <                                        -record_width*(j*ni_columns + i),
361 <                                        -record_width, 1, stdout);
360 >                                        (n_comp*comp_size)*(j*ni_columns + i),
361 >                                        n_comp*comp_size, 1, stdout);
362                  }
363              if (ferror(stdout)) {
364                  fprintf(stderr, "Error writing to stdout\n");
# Line 348 | Line 381 | do_resize(FILE *fp)
381          int     columns2go = no_columns;
382          char    word[256];
383                                                  /* sanity checks */
384 <        if (record_width <= 0) {
385 <                fprintf(stderr, "Bad call to do_resize (record_width = %d)\n",
353 <                                record_width);
354 <                return(0);
355 <        }
384 >        if (comp_size)
385 >                return(output_stream(fp));      /* binary data -- just copy */
386          if (no_columns <= 0) {
387                  fprintf(stderr, "Missing -oc specification\n");
388                  return(0);
# Line 368 | Line 398 | do_resize(FILE *fp)
398          do {                                    /* reshape records */
399                  int     n;
400  
401 <                for (n = record_width; n--; ) {
401 >                for (n = n_comp; n--; ) {
402                          if (fget_word(word, fp) == NULL) {
403 <                                if (records2go > 0 || n < record_width-1)
403 >                                if (records2go > 0 || n < n_comp-1)
404                                          break;
405                                  goto done;      /* normal EOD */
406                          }
# Line 405 | Line 435 | done:
435   static int
436   headline(char *s, void *p)
437   {
438 <        char    fmt[32];
438 >        static char     fmt[32];
439 >        int             n;
440  
441          if (formatval(fmt, s)) {
442 +                if (fmtid == NULL) {
443 +                        fmtid = fmt;
444 +                        return(0);
445 +                }
446                  if (!strcmp(fmt, fmtid))
447                          return(0);
448                  fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid);
449                  return(-1);
450          }
451 <        fputs(s, stdout);                       /* copy header info. */
451 >        if (!strncmp(s, "NROWS=", 6)) {
452 >                n = atoi(s+6);
453 >                if ((ni_rows > 0) & (n != ni_rows)) {
454 >                        fputs("Incorrect input row count\n", stderr);
455 >                        return(-1);
456 >                }
457 >                ni_rows = n;
458 >                return(0);
459 >        }
460 >        if (!strncmp(s, "NCOLS=", 6)) {
461 >                n = atoi(s+6);
462 >                if ((ni_columns > 0) & (n != ni_columns)) {
463 >                        fputs("Incorrect input column count\n", stderr);
464 >                        return(-1);
465 >                }
466 >                ni_columns = n;
467 >                return(0);
468 >        }
469 >        if (!strncmp(s, "NCOMP=", 6)) {
470 >                n = atoi(s+6);
471 >                if ((n_comp > 0) & (n != n_comp)) {
472 >                        fputs("Incorrect number of components\n", stderr);
473 >                        return(-1);
474 >                }
475 >                n_comp = n;
476 >                return(0);
477 >        }
478 >        if (o_header)
479 >                fputs(s, stdout);               /* copy header info. */
480          return(0);
481   }
482  
# Line 421 | Line 484 | headline(char *s, void *p)
484   int
485   main(int argc, char *argv[])
486   {
487 <        int     do_header = 1;                  /* header i/o? */
425 <        int     transpose = 0;                  /* transpose rows & cols? */
426 <        int     i;
487 >        int     a;
488  
489 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
490 <                switch (argv[i][1]) {
489 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
490 >                switch (argv[a][1]) {
491                  case 'i':                       /* input */
492 <                        if (argv[i][2] == 'c')  /* columns */
493 <                                ni_columns = atoi(argv[++i]);
494 <                        else if (argv[i][2] == 'r')
495 <                                ni_rows = atoi(argv[++i]);
492 >                        if (argv[a][2] == 'c')  /* columns */
493 >                                ni_columns = atoi(argv[++a]);
494 >                        else if (argv[a][2] == 'r')
495 >                                ni_rows = atoi(argv[++a]);
496                          else
497                                  goto userr;
498                          break;
499                  case 'o':                       /* output */
500 <                        if (argv[i][2] == 'c')  /* columns */
501 <                                no_columns = atoi(argv[++i]);
502 <                        else if (argv[i][2] == 'r')
503 <                                no_rows = atoi(argv[++i]);
500 >                        if (argv[a][2] == 'c')  /* columns */
501 >                                no_columns = atoi(argv[++a]);
502 >                        else if (argv[a][2] == 'r')
503 >                                no_rows = atoi(argv[++a]);
504                          else
505                                  goto userr;
506                          break;
507 <                case 'h':                       /* header on/off */
508 <                        do_header = !do_header;
507 >                case 'h':                       /* turn off header */
508 >                        switch (argv[a][2]) {
509 >                        case 'i':
510 >                                i_header = 0;
511 >                                break;
512 >                        case 'o':
513 >                                o_header = 0;
514 >                                break;
515 >                        case '\0':
516 >                                i_header = o_header = 0;
517 >                                break;
518 >                        default:
519 >                                goto userr;
520 >                        }
521                          break;
522                  case 't':                       /* transpose on/off */
523                          transpose = !transpose;
524                          break;
525                  case 'f':                       /* format */
526 <                        switch (argv[i][2]) {
526 >                        switch (argv[a][2]) {
527                          case 'a':               /* ASCII */
528                          case 'A':
529                                  fmtid = "ascii";
530 <                                record_width = 1;
530 >                                comp_size = 0;
531                                  break;
532                          case 'f':               /* float */
533                          case 'F':
534                                  fmtid = "float";
535 <                                record_width = -(int)sizeof(float);
535 >                                comp_size = sizeof(float);
536                                  break;
537                          case 'd':               /* double */
538                          case 'D':
539                                  fmtid = "double";
540 <                                record_width = -(int)sizeof(double);
540 >                                comp_size = sizeof(double);
541                                  break;
542                          case 'b':               /* binary (bytes) */
543                          case 'B':
544                                  fmtid = "byte";
545 <                                record_width = -1;
545 >                                comp_size = 1;
546                                  break;
547                          default:
548                                  goto userr;
549                          }
550 <                        if (argv[i][3]) {
551 <                                if (!isdigit(argv[i][3]))
550 >                        if (argv[a][3]) {
551 >                                if (!isdigit(argv[a][3]))
552                                          goto userr;
553 <                                record_width *= atoi(argv[i]+3);
554 <                        }
553 >                                n_comp = atoi(argv[a]+3);
554 >                        } else
555 >                                n_comp = 1;
556                          break;
557                  case 'w':                       /* warnings on/off */
558                          warnings = !warnings;
# Line 486 | Line 560 | main(int argc, char *argv[])
560                  default:
561                          goto userr;
562                  }
563 <        if (!record_width)
563 >        if (a < argc-1)                         /* arg count OK? */
564                  goto userr;
491        if (i < argc-1)                         /* arg count OK? */
492                goto userr;
565                                                  /* open input file? */
566 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
567 <                fprintf(stderr, "%s: cannot open for reading\n", argv[i]);
566 >        if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) {
567 >                fprintf(stderr, "%s: cannot open for reading\n", argv[a]);
568                  return(1);
569          }
570 <        if (record_width < 0) {
570 >        if (comp_size) {
571                  SET_FILE_BINARY(stdin);
572                  SET_FILE_BINARY(stdout);
573          }
574                                                  /* check for no-op */
575 <        if (!transpose && (record_width < 0 ||
575 >        if (!transpose & (i_header == o_header) && (comp_size ||
576                          (no_columns == ni_columns) & (no_rows == ni_rows))) {
577                  if (warnings)
578                          fprintf(stderr, "%s: no-op -- copying input verbatim\n",
# Line 509 | Line 581 | main(int argc, char *argv[])
581                          return(1);
582                  return(0);
583          }
584 <        if (do_header) {                        /* read/write header */
585 <                if (getheader(stdin, &headline, NULL) < 0)
584 >        if (i_header) {                         /* read header */
585 >                if (getheader(stdin, headline, NULL) < 0)
586                          return(1);
587 <                printargs(argc, argv, stdout);
587 >                if (!check_sizes())
588 >                        return(1);
589 >                if (comp_size) {                /* a little late... */
590 >                        SET_FILE_BINARY(stdin);
591 >                        SET_FILE_BINARY(stdout);
592 >                }
593 >        } else if (!check_sizes())
594 >                return(1);
595 >        if (o_header) {                         /* write header */
596 >                printargs(a, argv, stdout);
597 >                if (no_rows > 0)
598 >                        printf("NROWS=%d\n", no_rows);
599 >                if (no_columns > 0)
600 >                        printf("NCOLS=%d\n", no_columns);
601 >                printf("NCOMP=%d\n", n_comp);
602                  fputformat(fmtid, stdout);
603                  fputc('\n', stdout);            /* finish new header */
604          }
605          if (transpose) {                        /* transposing rows & columns? */
606                  MEMLOAD myMem;                  /* need to load into memory */
607 <                if (i == argc-1) {
607 >                if (a == argc-1) {
608                          if (load_file(&myMem, stdin) <= 0) {
609                                  fprintf(stderr, "%s: error loading file into memory\n",
610 <                                                argv[i]);
610 >                                                argv[a]);
611                                  return(1);
612                          }
613                  } else if (load_stream(&myMem, stdin) <= 0) {
# Line 537 | Line 623 | main(int argc, char *argv[])
623          return(0);
624   userr:
625          fprintf(stderr,
626 < "Usage: %s [-h][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
626 > "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n",
627                          argv[0]);
628          return(1);
629   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines