ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/util/rcomb.c
(Generate patch)

Comparing ray/src/util/rcomb.c (file contents):
Revision 2.21 by greg, Tue Jun 4 21:47:55 2024 UTC vs.
Revision 2.34 by greg, Tue Aug 5 16:40:10 2025 UTC

# Line 21 | Line 21 | static const char RCSid[] = "$Id$";
21   #define M_PI    3.14159265358979323846
22   #endif
23  
24 #define MAXCOMP         MAXCSAMP        /* #components we support */
25
24   /* Unary matrix operation(s) */
25   typedef struct {
26          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
# Line 62 | Line 60 | SUBPROC                *cproc = NULL;                  /* child process array */
60   int             nchildren = 0;                  /* # of child processes */
61   int             inchild = -1;                   /* our child ID (-1: parent) */
62  
63 < static int      checksymbolic(ROPMAT *rop);
63 > extern int      checksymbolic(ROPMAT *rop);
64  
65 < static int
65 > /* Split input matrices to allow for certain operations */
66 > int
67   split_input(ROPMAT *rop)
68   {
69          if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) {
# Line 76 | Line 75 | split_input(ROPMAT *rop)
75   }
76  
77   /* Check/set transform based on a reference input file */
78 < static int
78 > int
79   checkreffile(ROPMAT *rop)
80   {
81          static const char       *curRF = NULL;
# Line 139 | Line 138 | checkreffile(ROPMAT *rop)
138   }
139  
140   /* Compute conversion row from spectrum to one channel of RGB */
141 < static void
141 > void
142   rgbrow(ROPMAT *rop, int r, int p)
143   {
144          const int       nc = rop->imx.ncomp;
# Line 156 | Line 155 | rgbrow(ROPMAT *rop, int r, int p)
155   }
156  
157   /* Compute conversion row from spectrum to one channel of XYZ */
158 < static void
158 > void
159   xyzrow(ROPMAT *rop, int r, int p)
160   {
161          const int       nc = rop->imx.ncomp;
# Line 173 | Line 172 | xyzrow(ROPMAT *rop, int r, int p)
172   }
173  
174   /* Use the spectral sensitivity function to compute matrix coefficients */
175 < static void
176 < sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
175 > void
176 > sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4]))
177   {
178          const int       nc = rop->imx.ncomp;
179          int             i;
# Line 188 | Line 187 | sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in
187   }
188  
189   /* Check/set symbolic transform */
190 < static int
190 > int
191   checksymbolic(ROPMAT *rop)
192   {
193          const int       nc = rop->imx.ncomp;
# Line 196 | Line 195 | checksymbolic(ROPMAT *rop)
195          double          cf = 1;
196          int             i, j;
197                                          /* check suffix => reference file */
198 <        if (strchr(rop->preop.csym, '.') > rop->preop.csym)
198 >        if (strchr(rop->preop.csym, '.') != NULL)
199                  return(checkreffile(rop));
200  
201          if (nc < 3) {
# Line 302 | Line 301 | checksymbolic(ROPMAT *rop)
301          return(1);
302   }
303  
304 < static int
304 > /* Set up color transform for matrix */
305 > int
306   get_component_xfm(ROPMAT *rop)
307   {
308          int     i, j;
# Line 385 | Line 385 | get_component_xfm(ROPMAT *rop)
385          return(1);
386   }
387  
388 < static int
388 > /* Apply the given color transform and/or scaling operation */
389 > int
390   apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro)
391   {
392          if (ro->clen > 0) {
# Line 404 | Line 405 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
405          return(1);
406   }
407  
408 < static int
408 > /* Open the associated input file and load/check header */
409 > int
410   open_input(ROPMAT *rop)
411   {
412          int     outtype;
# Line 426 | Line 428 | open_input(ROPMAT *rop)
428   }
429  
430   /* Return nominal wavelength associated with input component (return nm) */
431 < static double
431 > double
432   l_wavelength(char *nam)
433   {
434          double  comp = argument(1);
# Line 450 | Line 452 | l_wavelength(char *nam)
452   }
453  
454   /* Return ith input with optional channel selector */
455 < static double
455 > double
456   l_chanin(char *nam)
457   {
458          double  inp = argument(1);
# Line 475 | Line 477 | l_chanin(char *nam)
477          return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]);
478   }
479  
480 < static int
480 > /* Set up our operations and check consistency */
481 > int
482   initialize(RMATRIX *imp)
483   {
484          int     i;
# Line 488 | Line 491 | initialize(RMATRIX *imp)
491                  restype = mop[i].rmp->dtype;
492                  if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0)
493                          imp->dtype = restype;
494 <                else
494 >                else if (!nowarn)
495                          fprintf(stderr, "%s: warning - data type mismatch\n",
496                                          mop[i].inspec);
497                  if (!i) {
# Line 522 | Line 525 | initialize(RMATRIX *imp)
525          return(1);
526   }
527  
528 < static void
528 > /* Copy input header information to output header, indented */
529 > void
530   output_headinfo(FILE *fp)
531   {
532          int     i;
# Line 545 | Line 549 | output_headinfo(FILE *fp)
549          }
550   }
551  
552 < static int
553 < output_loop(void)
550 < {
551 <        const size_t    row_size = rmx_array_size(mop[nmats].rmp);
552 <        int             i = nmats;
553 <        int             cur_child = 0;
554 <
555 <        if (mop[nmats].rmp != &mop[nmats].imx)          /* output is split? */
556 <                rmx_reset(&mop[nmats].imx);
557 <        while (i-- > 0) {                               /* close input matrices */
558 <                fclose(mop[i].infp);            /* ! pclose() */
559 <                mop[i].infp = NULL;
560 <                rmx_reset(&mop[i].imx);
561 <                if (mop[i].rmp != &mop[i].imx) {
562 <                        rmx_free(mop[i].rmp);
563 <                        mop[i].rmp = &mop[i].imx;
564 <                }
565 <        }
566 < #ifdef getc_unlocked
567 <        flockfile(stdout);                              /* we own this, now */
568 < #endif
569 <        for ( ; ; ) {                                   /* loop until no more */
570 <                ssize_t         rv;
571 <                rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size);
572 <                if (!rv)                                /* out of rows? */
573 <                        break;
574 <                if (rv != row_size) {
575 <                        fputs("Read error in output loop\n", stderr);
576 <                        return(0);
577 <                }                                       /* do final conversion */
578 <                if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp,
579 <                                mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) {
580 <                        fputs("Conversion/write error in output loop\n", stderr);
581 <                        return(0);
582 <                }
583 <                cur_child++;
584 <                cur_child *= (cur_child < inchild);
585 <        }
586 <        return(fflush(stdout) != EOF);
587 < }
588 <
589 < static int
552 > /* Spawn the indicated number of children and return 1 in parent */
553 > int
554   spawned_children(int np)
555   {
556          int     i, rv;
557  
558   #if defined(_WIN32) || defined(_WIN64)
559          if (np > 1) {
560 <                fputs("Warning: only one process under Windows\n", stderr);
560 >                if (!nowarn)
561 >                        fputs("Warning: only one process under Windows\n", stderr);
562                  np = 1;
563          } else
564   #endif
# Line 611 | Line 576 | spawned_children(int np)
576                  }
577          }
578                                  /* prep output row buffer(s) */
579 <        if (mcat || mop[nmats].preop.clen > 0) {
579 >        if (mop[nmats].preop.clen > 0) {
580                  if (!split_input(&mop[nmats]))  /* need separate buffer */
581 <                        return(0);
582 <                if (mop[nmats].preop.clen > 0)
583 <                        mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
619 <                                                mop[nmats].imx.ncomp;
581 >                        goto memerror;
582 >                mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
583 >                                        mop[nmats].imx.ncomp;
584          }
585          mop[nmats].imx.nrows = 1;
586          if (!rmx_prepare(&mop[nmats].imx))
# Line 641 | Line 605 | spawned_children(int np)
605                  goto memerror;
606          for (i = nchildren; i--; ) cproc[i] = sp_inactive;
607          cproc[nchildren-1].flags |= PF_FILT_OUT;
608 <                                /* start each child */
609 <        for (i = 0; i < nchildren; i++) {
610 <                rv = open_process(&cproc[i], NULL);
611 <                if (rv <= 0) break;
648 <        }
608 >                                /* start each child from parent */
609 >        for (i = 0; i < nchildren; i++)
610 >                if ((rv = open_process(&cproc[i], NULL)) <= 0)
611 >                        break;  /* child breaks here */
612          if (rv < 0) {
613 <                perror("fork");
613 >                perror("fork"); /* WTH? */
614                  close_processes(cproc, i);
615                  exit(1);
616          }
617 <        if (rv) {               /* are we the parent? */
618 <                i = nchildren-1;        /* last child is sole reader */
619 <                while (i-- > 0) {
620 <                        close(cproc[i].r);
621 <                        cproc[i].r = -1;
617 >        if (i != nchildren-1) { /* last child is sole reader */
618 >                int     j = i;
619 >                while (j-- > 0) {
620 >                        close(cproc[j].r);
621 >                        cproc[j].r = -1;
622                  }
660                return(1);      /* parent return value */
623          }
624 <        inchild = i;            /* our child index */
624 >        if (rv > 0)
625 >                return(1);      /* parent return value */
626 >
627 >        inchild = i;            /* else set our child index */
628          while (i-- > 0)         /* only parent writes siblings */
629                  close(cproc[i].w);
630  
631 <        if (inchild == nchildren-1)     /* last child sequences output */
632 <                exit(output_loop() ? 0 : 1);
631 >        i = nmats;              /* close matrix streams (carefully) */
632 >        while (i-- > 0) {
633 >                if (mop[i].infp != stdin) {
634 >                        close(fileno(mop[i].infp));     /* avoid lseek() */
635 >                        fclose(mop[i].infp);            /* ! pclose() */
636 >                }
637 >                mop[i].infp = NULL;
638 >        }
639 >        fpurge(stdin);          /* discard previously buffered input */
640  
641 <        i = inchild;            /* won't read from siblings */
642 <        while (i-- > 0)
643 <                close(cproc[i].r);
644 <        i = nmats;              /* redirect input matrix streams */
641 >        if (inchild == nchildren-1)
642 >                return(-1);     /* output process return value */
643 >
644 >        i = nmats;              /* get matrix rows from parent */
645          while (i-- > 0) {
674                if (mop[i].infp != stdin)
675                        fclose(mop[i].infp);    /* ! pclose() */
646                  mop[i].infp = stdin;
647                  mop[i].imx.dtype = DTrmx_native;
648                  mop[i].imx.pflags &= ~RMF_SWAPIN;
649          }
680        fpurge(stdin);          /* discard any previous matrix input */
650   #ifdef getc_unlocked
651          flockfile(stdin);
652   #endif
# Line 688 | Line 657 | memerror:
657          exit(1);
658   }
659  
660 < static int
660 > /* Run parental feeder loop */
661 > int
662   parent_loop(void)
663   {
664          int     i;
# Line 709 | Line 679 | parent_loop(void)
679                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
680                          if (cur_row > in_nrows) /* unknown #input rows? */
681                                  break;
682 <                        fprintf(stderr, "%s: parent read error at row %d\n",
682 >                        fprintf(stderr, "%s: load error at row %d\n",
683                                          mop[i].inspec, cur_row);
684                          return(0);
685                  }
# Line 717 | Line 687 | parent_loop(void)
687                  break;
688              for (i = 0; i < nmats; i++)
689                  if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx))
690 <                                        != rmx_array_size(&mop[i].imx))
690 >                                        != rmx_array_size(&mop[i].imx)) {
691 >                        fprintf(stderr, "%s: write error at row %d\n",
692 >                                        mop[i].inspec, cur_row);
693                          return(0);
694 +                }
695          }
696 <        i = close_processes(cproc, nchildren);
696 >        i = close_processes(cproc, nchildren);  /* collect family */
697          free(cproc); cproc = NULL; nchildren = 0;
698          if (i < 0) {
699 <                fputs("Warning: lost child in parent_loop()\n", stderr);
699 >                if (!nowarn)
700 >                        fputs("Warning: lost child process\n", stderr);
701                  return(1);
702          }
703          if (i > 0) {
# Line 731 | Line 705 | parent_loop(void)
705                  return(0);
706          }
707          return(1);                              /* return success! */
734 memerror:
735        fputs("Out of memory in parent_loop()\n", stderr);
736        exit(1);
708   }
709  
710 < static int
710 > /* Main operation loop, may be run in each child */
711 > int
712   combine_input(void)
713   {
714          const int       row0 = (inchild >= 0)*inchild;
# Line 747 | Line 719 | combine_input(void)
719          int             co_set;
720          int             i;
721  
722 <        if (mcat && mcat_last &&
723 <                        !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
724 <                goto memerror;
722 >        if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) {
723 >                fputs("Out of buffer space in combine_input()\n", stderr);
724 >                return(0);
725 >        }
726                                          /* figure out what the user set */
727          co_set = fundefined("co");
728          if (!co_set)
# Line 771 | Line 744 | combine_input(void)
744                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
745                          if (cur_row > in_nrows) /* unknown #input rows? */
746                                  break;
747 <                        fprintf(stderr, "%s: read error at row %d\n",
747 >                        fprintf(stderr, "%s: load error at row %d\n",
748                                          mop[i].inspec, cur_row);
749                          return(0);
750                  }
# Line 817 | Line 790 | combine_input(void)
790              }
791              rmx_free(mres); mres = NULL;
792              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
793 <                                res->rmp->ncols, res->rmp->dtype, stdout))
793 >                                res->rmp->ncols, res->rmp->dtype, stdout) ||
794 >                                 (inchild >= 0 && fflush(stdout) == EOF)) {
795 >                fprintf(stderr, "Conversion/write error at row %d\n",
796 >                                cur_row);
797                  return(0);
798 <            if (inchild >= 0 && fflush(stdout) == EOF)
823 <                return(0);
798 >            }
799          }
800          return(inchild >= 0 || fflush(stdout) != EOF);
826 memerror:
827        fputs("Out of buffer space in combine_input()\n", stderr);
828        return(0);
801   multerror:
802 <        fputs("Unexpected matrix multiply error in combine_input()\n", stderr);
802 >        fputs("Unexpected matrix multiply error\n", stderr);
803          return(0);
804   }
805  
806 < static int
806 > /* Run output process loop when #processes > 1 */
807 > int
808 > output_loop(void)
809 > {
810 >        const size_t    row_size = rmx_array_size(mop[nmats].rmp);
811 >        int             cur_child = 0;
812 >        int             i = nmats;
813 >
814 >        while (i-- > 0) {                               /* free input buffers */
815 >                rmx_reset(&mop[i].imx);
816 >                if (mop[i].rmp != &mop[i].imx) {
817 >                        rmx_free(mop[i].rmp);
818 >                        mop[i].rmp = &mop[i].imx;
819 >                }
820 >        }
821 >        if (mop[nmats].rmp != &mop[nmats].imx)          /* output is split? */
822 >                rmx_reset(&mop[nmats].imx);
823 > #ifdef getc_unlocked
824 >        flockfile(stdout);                              /* we own this, now */
825 > #endif
826 >        for ( ; ; ) {                                   /* loop until no more */
827 >                ssize_t         rv;
828 >                rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size);
829 >                if (!rv)                                /* out of rows? */
830 >                        break;
831 >                if (rv != row_size) {
832 >                        fputs("Read error\n", stderr);
833 >                        return(0);
834 >                }                                       /* do final conversion */
835 >                if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp,
836 >                                mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) {
837 >                        fputs("Conversion/write error\n", stderr);
838 >                        return(0);
839 >                }
840 >                cur_child++;
841 >                cur_child *= (cur_child < inchild);     /* loop over workers */
842 >        }
843 >        return(fflush(stdout) != EOF);
844 > }
845 >
846 > /* Check/convert floating-point arguments following this */
847 > int
848   get_factors(double da[], int n, char *av[])
849   {
850          int     ac;
# Line 841 | Line 854 | get_factors(double da[], int n, char *av[])
854          return(ac);
855   }
856  
857 < static void
857 > /* Resize/reallocate the input array as requested */
858 > void
859   resize_inparr(int n2alloc)
860   {
861          int     i;
862  
863          if (n2alloc == nall)
864                  return;
865 <        for (i = nall; i > n2alloc; i--) {
865 >        for (i = nall; i-- > n2alloc; ) {
866                  rmx_reset(&mop[i].imx);
867                  if (mop[i].rmp != &mop[i].imx)
868                          rmx_free(mop[i].rmp);
# Line 874 | Line 888 | main(int argc, char *argv[])
888          int             stdin_used = 0;
889          int             nproc = 1;
890          const char      *mcat_spec = NULL;
891 +        int             transpose_mcat = 0;
892          int             n2comp = 0;
893          uby8            comp_ndx[128];
894          int             i;
# Line 943 | Line 958 | main(int argc, char *argv[])
958                                  }
959                                  break;
960                          case 'C':
961 +                                mcat_last = 0;
962                                  if (!n || isflt(argv[i+1]))
963                                          goto userr;
964                                  defCsym = mop[nmats].preop.csym = argv[++i];
965                                  mop[nmats].preop.clen = 0;
950                                mcat_last = 0;
966                                  break;
967                          case 'c':
968 +                                mcat_last = 0;
969                                  if (n && !isflt(argv[i+1])) {
970                                          mop[nmats].preop.csym = argv[++i];
971                                          mop[nmats].preop.clen = 0;
956                                        mcat_last = 0;
972                                          break;
973                                  }
974                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
# Line 966 | Line 981 | main(int argc, char *argv[])
981                                          goto userr;
982                                  }
983                                  mop[nmats].preop.csym = NULL;
969                                mcat_last = 0;
984                                  break;
985                          case 'm':
986                                  if (!n) goto userr;
987 +                                mcat_last = 1;
988 +                                transpose_mcat = (argv[i][2] == 't');
989                                  if (argv[++i][0] == '-' && !argv[i][1]) {
990                                          if (stdin_used++) goto stdin_error;
991                                          mcat_spec = stdin_name;
992                                  } else
993                                          mcat_spec = argv[i];
978                                mcat_last = 1;
994                                  break;
995                          default:
996                                  fprintf(stderr, "%s: unknown option '%s'\n",
# Line 989 | Line 1004 | main(int argc, char *argv[])
1004          }
1005          resize_inparr(nmats+1);         /* extra matrix at end for result */
1006          mop[nmats].inspec = "trailing_ops";
1007 <                                        /* load final concatenation matrix */
1008 <        if (mcat_spec && !(mcat = rmx_load(mcat_spec, RMPnone))) {
1009 <                fprintf(stderr, "%s: error loading concatenation matrix: %s\n",
1010 <                                argv[0], mcat_spec);
1011 <                return(1);
1007 >
1008 >        if (mcat_spec) {                /* load final concatenation matrix? */
1009 >                mcat = rmx_load(mcat_spec);
1010 >                if (!mcat) {
1011 >                        fprintf(stderr, "%s: error loading concatenation matrix: %s\n",
1012 >                                        argv[0], mcat_spec);
1013 >                        return(1);
1014 >                }
1015 >                if (transpose_mcat && !rmx_transpose(mcat)) {
1016 >                        fprintf(stderr, "%s: error transposing concatenation matrix: %s\n",
1017 >                                        argv[0], mcat_spec);
1018 >                        return(1);
1019 >                }
1020          }
1021                                          /* get/check inputs, set constants */
1022          if (!initialize(&mop[nmats].imx))
# Line 1033 | Line 1056 | main(int argc, char *argv[])
1056                          return(1);
1057                  mop[nmats].rmp->ncols = mcat->ncols;
1058          }
1059 + #if DTrmx_native==DTfloat
1060 +        if (outfmt == DTdouble)
1061 +                fprintf(stderr, "%s: warning - writing float result as double\n", argv[0]);
1062 + #endif
1063          newheader("RADIANCE", stdout);  /* write output header */
1064          if (echoheader)
1065                  output_headinfo(stdout);
# Line 1044 | Line 1071 | main(int argc, char *argv[])
1071                  return(1);
1072          }
1073          doptimize(1);                   /* optimize definitions */
1074 <        if (spawned_children(nproc))    /* running in parent process? */
1074 >        i = spawned_children(nproc);    /* create multiple processes if requested */
1075 >        if (i > 0)                      /* running in parent process? */
1076                  return(parent_loop() ? 0 : 1);
1077 <                                        /* process & write rows */
1077 >        if (i < 0)                      /* running in output process? */
1078 >                return(output_loop() ? 0 : 1);
1079 >                                        /* else we are a worker process */
1080          return(combine_input() ? 0 : 1);
1081   stdin_error:
1082          fprintf(stderr, "%s: %s used for more than one input\n",

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)