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

Comparing ray/src/util/rcomb.c (file contents):
Revision 2.2 by greg, Mon Dec 18 23:04:05 2023 UTC vs.
Revision 2.11 by greg, Tue May 21 16:27:26 2024 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * General component matrix combiner, operating on a row at a time.
6   */
7  
8 #include <errno.h>
8   #include <math.h>
9   #include "platform.h"
10 + #include "rtprocess.h"
11   #include "rtio.h"
12 #include "resolu.h"
12   #include "rmatrix.h"
13   #include "calcomp.h"
15 #include "paths.h"
14  
15   #ifndef M_PI
16   #define M_PI    3.14159265358979323846
# Line 55 | Line 53 | int            cur_row;                        /* current input/output row */
53   int             cur_col;                        /* current input/output column */
54   int             cur_chan;                       /* if we're looping channels */
55  
56 + SUBPROC         *cproc = NULL;                  /* child process array */
57 + int             nchildren = 0;                  /* # of child processes */
58 + int             inchild = -1;                   /* our child ID (-1: parent) */
59 +
60   static int      checksymbolic(ROPMAT *rop);
61  
62   static int
# Line 186 | Line 188 | checksymbolic(ROPMAT *rop)
188   {
189          const int       nc = rop->imx.ncomp;
190          const int       dt = rop->imx.dtype;
191 +        double          cf = 1;
192          int             i, j;
193                                          /* check suffix => reference file */
194          if (strchr(rop->preop.csym, '.') > rop->preop.csym)
# Line 206 | Line 209 | checksymbolic(ROPMAT *rop)
209                  int     comp = 0;
210                  switch (rop->preop.csym[j]) {
211                  case 'B':
212 +                case 'b':
213                          ++comp;
214                          /* fall through */
215                  case 'G':
216 +                case 'g':
217                          ++comp;
218                          /* fall through */
219                  case 'R':
220 +                case 'r':
221 +                        if (rop->preop.csym[j] <= 'Z')
222 +                                cf = 1./WHTEFFICACY;
223                          if (dt == DTxyze) {
224                                  for (i = 3; i--; )
225 <                                        rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY *
218 <                                                        xyz2rgbmat[comp][i];
225 >                                        rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i];
226                          } else if (nc == 3)
227                                  rop->preop.cmat[j*nc+comp] = 1.;
228                          else
229                                  rgbrow(rop, j, comp);
230                          break;
231                  case 'Z':
232 +                case 'z':
233                          ++comp;
234                          /* fall through */
235                  case 'Y':
236 +                case 'y':
237                          ++comp;
238                          /* fall through */
239                  case 'X':
240 +                case 'x':
241 +                        if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze))
242 +                                cf = WHTEFFICACY;
243                          if (dt == DTxyze) {
244                                  rop->preop.cmat[j*nc+comp] = 1.;
245                          } else if (nc == 3) {
# Line 239 | Line 251 | checksymbolic(ROPMAT *rop)
251                          else
252                                  xyzrow(rop, j, comp);
253  
254 <                        for (i = nc*(dt != DTxyze); i--; )
255 <                                rop->preop.cmat[j*nc+i] *= WHTEFFICACY;
254 >                        for (i = nc*(cf != 1); i--; )
255 >                                rop->preop.cmat[j*nc+i] *= cf;
256                          break;
257                  case 'S':               /* scotopic (il)luminance */
258 +                        cf = WHTSCOTOPIC;
259 +                        /* fall through */
260 +                case 's':
261                          sensrow(rop, j, scolor2scotopic);
262 <                        for (i = nc; i--; )
263 <                                rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC;
262 >                        for (i = nc*(cf != 1); i--; )
263 >                                rop->preop.cmat[j*nc+i] *= cf;
264                          break;
265                  case 'M':               /* melanopic (il)luminance */
266 +                        cf = WHTMELANOPIC;
267 +                        /* fall through */
268 +                case 'm':
269                          sensrow(rop, j, scolor2melanopic);
270 <                        for (i = nc; i--; )
271 <                                rop->preop.cmat[j*nc+i] *= WHTMELANOPIC;
270 >                        for (i = nc*(cf != 1); i--; )
271 >                                rop->preop.cmat[j*nc+i] *= cf;
272                          break;
273                  case 'A':               /* average component */
274 +                case 'a':
275                          for (i = nc; i--; )
276                                  rop->preop.cmat[j*nc+i] = 1./(double)nc;
277                          break;
# Line 267 | Line 286 | checksymbolic(ROPMAT *rop)
286          memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart));
287          rop->rmp->ncomp = rop->preop.clen / nc;
288                                          /* decide on output type */
289 <        if (!strcmp(rop->preop.csym, "XYZ")) {
289 >        if (!strcasecmp(rop->preop.csym, "XYZ")) {
290                  if (dt <= DTspec)
291                          rop->rmp->dtype = DTxyze;
292 <        } else if (!strcmp(rop->preop.csym, "RGB")) {
292 >        } else if (!strcasecmp(rop->preop.csym, "RGB")) {
293                  if (dt <= DTspec)
294                          rop->rmp->dtype = DTrgbe;
295          } else if (rop->rmp->dtype == DTspec)
# Line 374 | Line 393 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
393                          return(0);
394                  rmx_free(res);
395          } else if (dst != src)
396 <                memcpy(dst->mtx, src->mtx,
378 <                                sizeof(double)*dst->ncomp*dst->ncols*dst->nrows);
396 >                memcpy(dst->mtx, src->mtx, rmx_array_size(dst));
397          if (ro->nsf == dst->ncomp)
398                  rmx_scale(dst, ro->sca);
399          return(1);
# Line 523 | Line 541 | output_headinfo(FILE *fp)
541   }
542  
543   static int
544 < combine_input(ROPMAT *res, FILE *fout)
544 > spawned_children(int np)
545   {
546 <        int     set_r, set_c;
547 <        RMATRIX *tmp = NULL;
548 <        int     co_set;
549 <        int     i;
550 <                                        /* allocate input row buffers */
546 >        int     i, rv;
547 >
548 > #if defined(_WIN32) || defined(_WIN64)
549 >        if (np > 1) {
550 >                fputs("Warning: only one process under Windows\n", stderr);
551 >                np = 1;
552 >        } else
553 > #endif
554 >        if ((in_nrows > 0) & (np > in_nrows))
555 >                np = in_nrows;
556 >                                /* we'll be doing a row at a time */
557          for (i = 0; i < nmats; i++) {
558 <                mop[i].imx.nrows = 1;   /* we'll be doing a row at a time */
558 >                mop[i].imx.nrows = 1;
559                  if (!rmx_prepare(&mop[i].imx))
560                          goto memerror;
561                  if (mop[i].rmp != &mop[i].imx) {
# Line 540 | Line 564 | combine_input(ROPMAT *res, FILE *fout)
564                                  goto memerror;
565                  }
566          }
567 <                                        /* prep output row buffers */
568 <        if (mcat || res->preop.clen > 0) {
569 <                if (!split_input(res))  /* need separate buffer */
567 >                                /* prep output row buffer */
568 >        if (mcat || mop[nmats].preop.clen > 0) {
569 >                if (!split_input(&mop[nmats]))  /* need separate buffer */
570                          return(0);
571 <                if (res->preop.clen > 0)
572 <                        res->rmp->ncomp = res->preop.clen / res->imx.ncomp;
573 <                res->rmp->nrows = 1;
574 <                if (!mcat | !mcat_last && !rmx_prepare(res->rmp))
571 >                if (mop[nmats].preop.clen > 0)
572 >                        mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
573 >                                                mop[nmats].imx.ncomp;
574 >                mop[nmats].rmp->nrows = 1;
575 >                if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp))
576                          goto memerror;
577          }
578 +        mop[nmats].imx.nrows = 1;
579 +        if (!rmx_prepare(&mop[nmats].imx))
580 +                goto memerror;
581 +        if (np <= 1) {          /* single process return point */
582 + #ifdef getc_unlocked
583 +                for (i = 0; i < nmats; i++)
584 +                        flockfile(mop[i].infp);
585 +                flockfile(stdout);
586 + #endif
587 +                return(0);
588 +        }
589 +        fflush(stdout);         /* flush header & spawn children */
590 +        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np);
591 +        if (!cproc)
592 +                goto memerror;
593 +        nchildren = np;
594 +        for (i = 0; i < np; i++) {
595 +                cproc[i].flags = PF_FILT_OUT;
596 +                cproc[i].w = dup(1);
597 +                cproc[i].r = 0;
598 +                cproc[i].pid = -1;
599 +                rv = open_process(&cproc[i], NULL);
600 +                if (rv <= 0) break;
601 +        }
602 +        if (rv > 0)
603 +                return(1);      /* parent return value */
604 +        if (rv < 0) {
605 +                perror("fork");
606 +                exit(1);
607 +        }
608 +        inchild = i;            /* our child index */
609 +        while (i-- > 0)         /* don't share siblings' pipes */
610 +                close(cproc[i].w);
611 +        fpurge(stdin);          /* discard previous matrix input */
612 + #ifdef getc_unlocked
613 +        flockfile(stdin);
614 + #endif
615 +        for (i = 0; i < nmats; i++) {
616 +                if (mop[i].infp != stdin)
617 +                        fclose(mop[i].infp);    /* ! pclose() */
618 +                mop[i].infp = stdin;
619 +                mop[i].imx.dtype = DTdouble;
620 +        }
621 +        return(0);              /* child return */
622 + memerror:
623 +        fputs("Out of memory in spawned_children()\n", stderr);
624 +        exit(1);
625 + }
626 +
627 + static int
628 + parent_loop()
629 + {
630 +        FILE    **outfp = (FILE **)malloc(nchildren*sizeof(FILE *));
631 +        int     i;
632 +
633 +        if (!outfp) goto memerror;
634 +        for (i = 0; i < nchildren; i++) {
635 +                outfp[i] = fdopen(cproc[i].w, "w");
636 +                if (!outfp[i]) goto memerror;
637 + #ifdef getc_unlocked
638 +                flockfile(outfp[i]);
639 + #endif
640 +        }
641 + #ifdef getc_unlocked
642 +        for (i = 0; i < nmats; i++)
643 +                flockfile(mop[i].infp);
644 + #endif
645 +        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
646 +            FILE        *ofp = outfp[cur_row % nchildren];
647 +            for (i = 0; i < nmats; i++)
648 +                if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
649 +                        if (cur_row > in_nrows) /* unknown #input rows? */
650 +                                break;
651 +                        fprintf(stderr, "%s: read error at row %d\n",
652 +                                        mop[i].inspec, cur_row);
653 +                        return(0);
654 +                }
655 +            if (i < nmats)
656 +                break;
657 +            for (i = 0; i < nmats; i++)
658 +                if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp,
659 +                                mop[i].imx.ncols, DTdouble, ofp))
660 +                        return(0);
661 +            if (fflush(ofp) == EOF)
662 +                return(0);
663 +        }
664 +        for (i = 0; i < nchildren; i++)
665 +                fclose(outfp[i]);
666 +        free(outfp);
667 +        i = close_processes(cproc, nchildren);
668 +        free(cproc); cproc = NULL;
669 +        if (i < 0) {
670 +                fputs("Warning: missing child in parent_loop()\n", stderr);
671 +                return(1);
672 +        }
673 +        if (i > 0) {
674 +                fprintf(stderr, "Child exited with status %d\n", i);
675 +                return(0);
676 +        }
677 +        return(1);
678 + memerror:
679 +        fputs("Out of memory in parent_loop()\n", stderr);
680 +        exit(1);
681 + }
682 +
683 + static int
684 + combine_input()
685 + {
686 +        const int       row0 = (inchild >= 0)*inchild;
687 +        const int       rstep = nchildren + !nchildren;
688 +        ROPMAT          *res = &mop[nmats];
689 +        int             set_r, set_c;
690 +        RMATRIX         *tmp = NULL;
691 +        int             co_set;
692 +        int             i;
693 +
694          if (mcat && mcat_last &&
695                          !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
696                  goto memerror;
556        res->imx.nrows = 1;
557        if (!rmx_prepare(&res->imx))
558                goto memerror;
697                                          /* figure out what the user set */
698          co_set = fundefined("co");
699          if (!co_set)
# Line 571 | Line 709 | combine_input(ROPMAT *res, FILE *fout)
709          } else                          /* save a little time */
710                  set_r = set_c = 0;
711                                          /* read/process row-by-row */
712 <        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
712 >        for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) {
713              RMATRIX     *mres = NULL;
714 <            for (i = 0; i < nmats; i++) {
714 >            for (i = 0; i < nmats; i++)
715                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
716 <                        if (in_nrows <= 0)      /* normal end? */
717 <                                goto loop_exit;
716 >                        if (cur_row > in_nrows) /* unknown #input rows? */
717 >                                break;
718                          fprintf(stderr, "%s: read error at row %d\n",
719                                          mop[i].inspec, cur_row);
720                          return(0);
721                  }
722 +            if (i < nmats)
723 +                break;
724 +            for (i = 0; i < nmats; i++)
725                  if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop))
726                          return(0);
586            }
727              if (set_r) varset("r", '=', cur_row);
728              for (cur_col = 0; cur_col < in_ncols; cur_col++) {
729                  if (set_c) varset("c", '=', cur_col);
# Line 620 | Line 760 | combine_input(ROPMAT *res, FILE *fout)
760                          return(0);
761              }
762              rmx_free(mres); mres = NULL;
763 +            if (inchild >= 0) {
764 +                i = getc(stdin);        /* child waits for turn to output */
765 +                if (i != EOF) ungetc(i, stdin);
766 +            }
767              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
768 <                                res->rmp->ncols, res->rmp->dtype, fout))
768 >                                res->rmp->ncols, res->rmp->dtype, stdout))
769                  return(0);
770 +            if (inchild >= 0 && fflush(stdout) == EOF)
771 +                return(0);
772          }
773 < loop_exit:
628 < #if 0           /* we're about to exit, so who cares? */
629 <        rmx_free(tmp);                  /* clean up */
630 <        rmx_reset(res->rmp);
631 <        rmx_reset(&res->imx);
632 <        for (i = 0; i < nmats; i++) {
633 <                rmx_reset(mop[i].rmp);
634 <                rmx_reset(&mop[i].imx);
635 <                if (mop[i].inspec[0] == '!')
636 <                        pclose(mop[i].infp);
637 <                else if (mop[i].inspec != stdin_name)
638 <                        fclose(mop[i].infp);
639 <                mop[i].infp = NULL;
640 <        }
641 < #endif
642 <        return(fflush(fout) != EOF);
773 >        return(inchild >= 0 || fflush(stdout) != EOF);
774   memerror:
775          fputs("Out of buffer space in combine_input()\n", stderr);
776          return(0);
# Line 663 | Line 794 | resize_inparr(int n2alloc)
794   {
795          int     i;
796  
797 <        for (i = nmats; i > n2alloc; i--) {
797 >        if (n2alloc == nall)
798 >                return;
799 >        for (i = nall; i > n2alloc; i--) {
800                  rmx_reset(&mop[i].imx);
801                  if (mop[i].rmp != &mop[i].imx)
802                          rmx_free(mop[i].rmp);
# Line 673 | Line 806 | resize_inparr(int n2alloc)
806                  fputs("Out of memory in resize_inparr()\n", stderr);
807                  exit(1);
808          }
809 <        if (n2alloc > nmats)
810 <                memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
809 >        if (n2alloc > nall)
810 >                memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT));
811          nall = n2alloc;
812   }
813  
# Line 687 | Line 820 | main(int argc, char *argv[])
820          const char      *defCsym = NULL;
821          int             echoheader = 1;
822          int             stdin_used = 0;
823 +        int             nproc = 1;
824          const char      *mcat_spec = NULL;
825          int             n2comp = 0;
826          uby8            comp_ndx[128];
# Line 714 | Line 848 | main(int argc, char *argv[])
848                          case 'h':
849                                  echoheader = !echoheader;
850                                  break;
851 +                        case 'n':
852 +                                nproc = atoi(argv[++i]);
853 +                                if (nproc <= 0)
854 +                                        goto userr;
855 +                                break;
856                          case 'e':
857                                  if (!n) goto userr;
858                                  comp_ndx[n2comp++] = i++;
# Line 851 | Line 990 | main(int argc, char *argv[])
990                  fprintf(stderr, "%s: unsupported output format\n", argv[0]);
991                  return(1);
992          }
993 +        doptimize(1);                   /* optimize definitions */
994 +        if (spawned_children(nproc))    /* running in parent process? */
995 +                return(parent_loop() ? 0 : 1);
996                                          /* process & write rows */
997 <        return(combine_input(&mop[nmats], stdout) ? 0 : 1);
997 >        return(combine_input() ? 0 : 1);
998   stdin_error:
999          fprintf(stderr, "%s: %s used for more than one input\n",
1000                          argv[0], stdin_name);
1001          return(1);
1002   userr:
1003          fprintf(stderr,
1004 <        "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1004 >        "Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1005                          argv[0]);
1006          return(1);
1007   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines