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

Comparing ray/src/util/rmtxop.c (file contents):
Revision 2.23 by greg, Tue Nov 28 16:36:50 2023 UTC vs.
Revision 2.28 by greg, Sun Dec 3 02:28:33 2023 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * General component matrix operations.
6   */
7  
8 #include <stdlib.h>
8   #include <errno.h>
10 #include <ctype.h>
9   #include "rtio.h"
10   #include "resolu.h"
11   #include "rmatrix.h"
# Line 17 | Line 15 | static const char RCSid[] = "$Id$";
15  
16   /* Unary matrix operation(s) */
17   typedef struct {
20        double          sca[MAXCOMP];           /* scalar coefficients */
18          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
19 <        short           nsf;                    /* number of scalars */
19 >        double          sca[MAXCOMP];           /* scalar coefficients */
20 >        const char      *csym;                  /* symbolic coefs or file */
21          short           clen;                   /* number of coefficients */
22 <        char            csym[11];               /* symbolic coefficients */
23 <        char            transpose;              /* do transpose? */
22 >        short           nsf;                    /* number of scalars */
23 >        short           transpose;              /* do transpose? */
24   } RUNARYOP;
25  
26   /* Matrix input source and requested operation(s) */
# Line 48 | Line 46 | loadmatrix(ROPMAT *rop)
46          return(!rop->mtx ? -1 : 1);
47   }
48  
49 + static int      checksymbolic(ROPMAT *rop);
50 +
51 + /* Check/set transform based on a reference input file */
52 + static int
53 + checkreffile(ROPMAT *rop)
54 + {
55 +        static const char       *curRF = NULL;
56 +        static RMATRIX          refm;
57 +        const int               nc = rop->mtx->ncomp;
58 +        int                     i;
59 +
60 +        if (!curRF || strcmp(rop->preop.csym, curRF)) {
61 +                FILE    *fp = fopen(rop->preop.csym, "rb");
62 +                if (!rmx_load_header(&refm, fp)) {
63 +                        fprintf(stderr, "%s: cannot read info header\n",
64 +                                        rop->preop.csym);
65 +                        curRF = NULL;
66 +                        if (fp) fclose(fp);
67 +                        return(-1);
68 +                }
69 +                fclose(fp);
70 +                curRF = rop->preop.csym;
71 +        }
72 +        if ((refm.ncomp == 3) & (refm.dtype != DTspec)) {
73 +                rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB";
74 +                return(checksymbolic(rop));
75 +        }
76 +        if (refm.ncomp == 2) {
77 +                fprintf(stderr, "%s: cannot convert to 2 components\n",
78 +                                curRF);
79 +                return(-1);
80 +        }
81 +        if (refm.ncomp == 1) {
82 +                rop->preop.csym = "Y";          /* XXX big assumption */
83 +                return(checksymbolic(rop));
84 +        }
85 +        if (refm.ncomp == nc &&
86 +                        !memcmp(refm.wlpart, rop->mtx->wlpart, sizeof(refm.wlpart)))
87 +                return(0);                      /* nothing to do */
88 +
89 +        if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) {
90 +                fprintf(stderr, "%s: cannot resample from %d to %d components\n",
91 +                                curRF, nc, refm.ncomp);
92 +                return(-1);
93 +        }
94 +        rop->preop.clen = refm.ncomp * nc;      /* compute spec to ref */
95 +
96 +        for (i = 0; i < nc; i++) {
97 +                SCOLOR  scstim, scresp;
98 +                int     j;
99 +                memset(scstim, 0, sizeof(COLORV)*nc);
100 +                scstim[i] = 1.f;
101 +                convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3],
102 +                                scstim, nc, rop->mtx->wlpart[0], rop->mtx->wlpart[3]);
103 +                for (j = refm.ncomp; j-- > 0; )
104 +                        rop->preop.cmat[j*nc + i] = scresp[j];
105 +        }
106 +        return(0);
107 + }
108 +
109   /* Compute conversion row from spectrum to one channel of RGB */
110   static void
111   rgbrow(ROPMAT *rop, int r, int p)
# Line 91 | Line 149 | sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in
149  
150          for (i = nc; i--; ) {
151                  SCOLOR  sclr;
152 <                scolorblack(sclr);
153 <                sclr[i] = 1;
152 >                memset(sclr, 0, sizeof(COLORV)*nc);
153 >                sclr[i] = 1.f;
154                  rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->mtx->wlpart);
155          }
156   }
# Line 104 | Line 162 | checksymbolic(ROPMAT *rop)
162          const int       nc = rop->mtx->ncomp;
163          const int       dt = rop->mtx->dtype;
164          int             i, j;
165 +                                        /* check suffix => reference file */
166 +        if (strchr(rop->preop.csym, '.') > rop->preop.csym)
167 +                return(checkreffile(rop));
168  
169          if (nc < 3) {
170                  fprintf(stderr, "%s: -c '%s' requires at least 3 components\n",
# Line 156 | Line 217 | checksymbolic(ROPMAT *rop)
217                          for (i = nc*(dt != DTxyze); i--; )
218                                  rop->preop.cmat[j*nc+i] *= WHTEFFICACY;
219                          break;
220 <                case 'S':
220 >                case 'S':               /* scotopic (il)luminance */
221                          sensrow(rop, j, scolor2scotopic);
222                          for (i = nc; i--; )
223                                  rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC;
224                          break;
225 <                case 'M':
225 >                case 'M':               /* melanopic (il)luminance */
226                          sensrow(rop, j, scolor2melanopic);
227                          for (i = nc; i--; )
228                                  rop->preop.cmat[j*nc+i] *= WHTMELANOPIC;
229                          break;
230 +                case 'A':               /* average component */
231 +                        for (i = nc; i--; )
232 +                                rop->preop.cmat[j*nc+i] = 1./(double)nc;
233 +                        break;
234                  default:
235                          fprintf(stderr, "%s: -c '%c' unsupported\n",
236                                  rop->inspec, rop->preop.csym[j]);
# Line 196 | Line 261 | loadop(ROPMAT *rop)
261          if (loadmatrix(rop) < 0)                /* make sure we're loaded */
262                  return(NULL);
263  
264 <        if (rop->preop.csym[0] &&               /* symbolic transform? */
264 >        if (rop->preop.csym &&                  /* symbolic transform? */
265                          (outtype = checksymbolic(rop)) < 0)
266                  goto failure;
267          if (rop->preop.clen > 0) {              /* apply component transform? */
# Line 209 | Line 274 | loadop(ROPMAT *rop)
274                          if (rop->preop.nsf == 1) {
275                                  for (i = rop->preop.clen; i--; )
276                                          rop->preop.cmat[i] *= rop->preop.sca[0];
277 <                        } else if (rop->preop.nsf != rop->mtx->ncomp) {
277 >                        } else if (rop->preop.nsf*rop->mtx->ncomp != rop->preop.clen) {
278                                  fprintf(stderr, "%s: -s must have one or %d factors\n",
279 <                                                rop->inspec, rop->mtx->ncomp);
279 >                                                rop->inspec,
280 >                                                rop->preop.clen/rop->mtx->ncomp);
281                                  goto failure;
282                          } else {
283 <                                for (j = rop->preop.clen/rop->preop.nsf; j--; )
284 <                                        for (i = rop->preop.nsf; i--; )
285 <                                                rop->preop.cmat[j*rop->preop.nsf+i] *=
286 <                                                                rop->preop.sca[i];
283 >                                for (i = rop->preop.nsf; i--; )
284 >                                        for (j = rop->mtx->ncomp; j--; )
285 >                                                rop->preop.cmat[i*rop->mtx->ncomp+j]
286 >                                                                *= rop->preop.sca[i];
287                          }
288                  }
289                  mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp,
# Line 232 | Line 298 | loadop(ROPMAT *rop)
298                                          rop->inspec, mres->ncomp,
299                                          rop->mtx->ncomp,
300                                          rop->preop.nsf ? " (* scalar)" : "");
301 <                rop->preop.nsf = 0;
301 >                rop->preop.nsf = 0;             /* now folded in */
302                  if ((mres->ncomp > 3) & (mres->dtype <= DTspec))
303                          outtype = DTfloat;      /* probably not actual spectrum */
304                  rmx_free(rop->mtx);
# Line 445 | Line 511 | get_factors(double da[], int n, char *av[])
511   }
512  
513   static ROPMAT *
514 < grow_moparray(ROPMAT *mop, int n2alloc)
514 > resize_moparr(ROPMAT *mop, int n2alloc)
515   {
516          int     nmats = 0;
517 +        int     i;
518  
519          while (mop[nmats++].binop)
520                  ;
521 +        for (i = nmats; i > n2alloc; i--)
522 +                rmx_free(mop[i].mtx);
523          mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
524          if (mop == NULL) {
525 <                fputs("Out of memory in grow_moparray()\n", stderr);
525 >                fputs("Out of memory in resize_moparr()\n", stderr);
526                  exit(1);
527          }
528          if (n2alloc > nmats)
# Line 465 | Line 534 | grow_moparray(ROPMAT *mop, int n2alloc)
534   int
535   main(int argc, char *argv[])
536   {
537 <        int     outfmt = DTfromHeader;
538 <        int     nall = 2;
539 <        ROPMAT  *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
540 <        int     nmats = 0;
541 <        RMATRIX *mres = NULL;
542 <        int     stdin_used = 0;
543 <        int     i;
537 >        int             outfmt = DTfromHeader;
538 >        const char      *defCsym = NULL;
539 >        int             nall = 2;
540 >        ROPMAT          *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
541 >        int             nmats = 0;
542 >        RMATRIX         *mres = NULL;
543 >        int             stdin_used = 0;
544 >        int             i;
545                                          /* get options and arguments */
546          for (i = 1; i < argc; i++) {
547                  if (argv[i][0] && !argv[i][1] &&
# Line 494 | Line 564 | main(int argc, char *argv[])
564                                  mop[nmats].inspec = stdin_name;
565                          } else
566                                  mop[nmats].inspec = argv[i];
567 +                        if (!mop[nmats].preop.csym)
568 +                                mop[nmats].preop.csym = defCsym;
569                          if (nmats > 0 && !mop[nmats-1].binop)
570                                  mop[nmats-1].binop = '.';
571                          nmats++;
# Line 535 | Line 607 | main(int argc, char *argv[])
607                                          goto userr;
608                                  }
609                                  break;
610 +                        case 'C':
611 +                                if (!n || isflt(argv[i+1]))
612 +                                        goto userr;
613 +                                defCsym = mop[nmats].preop.csym = argv[++i];
614 +                                mop[nmats].preop.clen = 0;
615 +                                break;
616                          case 'c':
617 <                                if (n && isupper(argv[i+1][0])) {
618 <                                        strlcpy(mop[nmats].preop.csym,
541 <                                                argv[++i],
542 <                                                sizeof(mop[0].preop.csym));
617 >                                if (n && !isflt(argv[i+1])) {
618 >                                        mop[nmats].preop.csym = argv[++i];
619                                          mop[nmats].preop.clen = 0;
620                                          break;
621                                  }
# Line 552 | Line 628 | main(int argc, char *argv[])
628                                                          argv[0]);
629                                          goto userr;
630                                  }
631 <                                mop[nmats].preop.csym[0] = '\0';
631 >                                mop[nmats].preop.csym = NULL;
632                                  break;
633                          case 'r':
634                                  if (argv[i][2] == 'f')
# Line 569 | Line 645 | main(int argc, char *argv[])
645                          }
646                  }
647                  if (nmats >= nall)
648 <                        mop = grow_moparray(mop, nall += 2);
648 >                        mop = resize_moparr(mop, nall += 2);
649          }
650          if (mop[0].inspec == NULL)      /* nothing to do? */
651                  goto userr;
# Line 597 | Line 673 | main(int argc, char *argv[])
673                  else if (mres->dtype == DTxyze)
674                          outfmt = DTxyze;
675          }
676 <        if (outfmt != DTascii)          /* write result to stdout */
601 <                SET_FILE_BINARY(stdout);
602 <        newheader("RADIANCE", stdout);
676 >        newheader("RADIANCE", stdout);  /* write result to stdout */
677          printargs(argc, argv, stdout);
604
678          return(rmx_write(mres, outfmt, stdout) ? 0 : 1);
679   userr:
680          fprintf(stderr,
681 <        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n",
681 >        "Usage: %s [-v][-f{adfc}][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n",
682                          argv[0]);
683          return(1);
684   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines