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.28 by greg, Sun Dec 3 02:28:33 2023 UTC vs.
Revision 2.41 by greg, Fri Apr 18 23:59:03 2025 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7  
8   #include <errno.h>
9   #include "rtio.h"
10 #include "resolu.h"
10   #include "rmatrix.h"
11   #include "platform.h"
12  
13 < #define MAXCOMP         MAXCSAMP        /* #components we support */
13 > /* Preferred BSDF component:
14 >        none, transmission, reflection front (normal side), reflection back */
15 > typedef enum {RMPnone=-1, RMPtrans=0, RMPreflF, RMPreflB} RMPref;
16  
17   /* Unary matrix operation(s) */
18   typedef struct {
# Line 35 | Line 36 | typedef struct {
36   int     verbose = 0;                    /* verbose reporting? */
37  
38   /* Load matrix */
39 < static int
39 > int
40   loadmatrix(ROPMAT *rop)
41   {
42 <        if (rop->mtx != NULL)           /* already loaded? */
42 >        if (rop->mtx)                   /* already loaded? */
43                  return(0);
44 +                                        /* check for BSDF input */
45 +        if ((rop->inspec[0] != '!') & (rop->rmp != RMPnone)) {
46 +                const char      *sp = strrchr(rop->inspec, '.');
47 +                if (sp > rop->inspec && !strcasecmp(sp+1, "XML")) {
48 +                        CMATRIX *cm = rop->rmp==RMPtrans ? cm_loadBTDF(rop->inspec) :
49 +                                        cm_loadBRDF(rop->inspec, rop->rmp==RMPreflB) ;
50 +                        if (!cm)
51 +                                return(-1);
52 +                        rop->mtx = rmx_from_cmatrix(cm);
53 +                        cm_free(cm);
54 +                        rop->mtx->dtype = DTascii;
55 +                        return(1);      /* loaded BSDF XML file */
56 +                }
57 +        }                               /* else load regular matrix */
58 +        rop->mtx = rmx_load(rop->inspec);
59  
60 <        rop->mtx = rmx_load(rop->inspec, rop->rmp);
45 <
46 <        return(!rop->mtx ? -1 : 1);
60 >        return(rop->mtx ? 1 : -1);
61   }
62  
63 < static int      checksymbolic(ROPMAT *rop);
63 > extern int      checksymbolic(ROPMAT *rop);
64  
65   /* Check/set transform based on a reference input file */
66 < static int
66 > int
67   checkreffile(ROPMAT *rop)
68   {
69          static const char       *curRF = NULL;
# Line 69 | Line 83 | checkreffile(ROPMAT *rop)
83                  fclose(fp);
84                  curRF = rop->preop.csym;
85          }
86 <        if ((refm.ncomp == 3) & (refm.dtype != DTspec)) {
86 >        if (refm.ncomp == 3) {
87                  rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB";
88                  return(checksymbolic(rop));
89          }
# Line 103 | Line 117 | checkreffile(ROPMAT *rop)
117                  for (j = refm.ncomp; j-- > 0; )
118                          rop->preop.cmat[j*nc + i] = scresp[j];
119          }
120 +        memcpy(rop->mtx->wlpart, refm.wlpart, sizeof(rop->mtx->wlpart));
121          return(0);
122   }
123  
124   /* Compute conversion row from spectrum to one channel of RGB */
125 < static void
125 > void
126   rgbrow(ROPMAT *rop, int r, int p)
127   {
128          const int       nc = rop->mtx->ncomp;
# Line 124 | Line 139 | rgbrow(ROPMAT *rop, int r, int p)
139   }
140  
141   /* Compute conversion row from spectrum to one channel of XYZ */
142 < static void
142 > void
143   xyzrow(ROPMAT *rop, int r, int p)
144   {
145          const int       nc = rop->mtx->ncomp;
# Line 141 | Line 156 | xyzrow(ROPMAT *rop, int r, int p)
156   }
157  
158   /* Use the spectral sensitivity function to compute matrix coefficients */
159 < static void
160 < sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
159 > void
160 > sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4]))
161   {
162          const int       nc = rop->mtx->ncomp;
163          int             i;
# Line 156 | Line 171 | sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in
171   }
172  
173   /* Check/set symbolic transform */
174 < static int
174 > int
175   checksymbolic(ROPMAT *rop)
176   {
177          const int       nc = rop->mtx->ncomp;
178          const int       dt = rop->mtx->dtype;
179 +        double          cf = 1;
180          int             i, j;
181                                          /* check suffix => reference file */
182          if (strchr(rop->preop.csym, '.') > rop->preop.csym)
# Line 181 | Line 197 | checksymbolic(ROPMAT *rop)
197                  int     comp = 0;
198                  switch (rop->preop.csym[j]) {
199                  case 'B':
200 +                case 'b':
201                          ++comp;
202                          /* fall through */
203                  case 'G':
204 +                case 'g':
205                          ++comp;
206                          /* fall through */
207                  case 'R':
208 +                case 'r':
209 +                        if (rop->preop.csym[j] <= 'Z')
210 +                                cf = 1./WHTEFFICACY;
211                          if (dt == DTxyze) {
212                                  for (i = 3; i--; )
213 <                                        rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY *
193 <                                                        xyz2rgbmat[comp][i];
213 >                                        rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i];
214                          } else if (nc == 3)
215                                  rop->preop.cmat[j*nc+comp] = 1.;
216                          else
217                                  rgbrow(rop, j, comp);
218                          break;
219                  case 'Z':
220 +                case 'z':
221                          ++comp;
222                          /* fall through */
223                  case 'Y':
224 +                case 'y':
225                          ++comp;
226                          /* fall through */
227                  case 'X':
228 +                case 'x':
229 +                        if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze))
230 +                                cf = WHTEFFICACY;
231                          if (dt == DTxyze) {
232                                  rop->preop.cmat[j*nc+comp] = 1.;
233                          } else if (nc == 3) {
# Line 214 | Line 239 | checksymbolic(ROPMAT *rop)
239                          else
240                                  xyzrow(rop, j, comp);
241  
242 <                        for (i = nc*(dt != DTxyze); i--; )
243 <                                rop->preop.cmat[j*nc+i] *= WHTEFFICACY;
242 >                        for (i = nc*(cf != 1); i--; )
243 >                                rop->preop.cmat[j*nc+i] *= cf;
244                          break;
245                  case 'S':               /* scotopic (il)luminance */
246 +                        cf = WHTSCOTOPIC;
247 +                        /* fall through */
248 +                case 's':
249                          sensrow(rop, j, scolor2scotopic);
250 <                        for (i = nc; i--; )
251 <                                rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC;
250 >                        for (i = nc*(cf != 1); i--; )
251 >                                rop->preop.cmat[j*nc+i] *= cf;
252                          break;
253                  case 'M':               /* melanopic (il)luminance */
254 +                        cf = WHTMELANOPIC;
255 +                        /* fall through */
256 +                case 'm':
257                          sensrow(rop, j, scolor2melanopic);
258 <                        for (i = nc; i--; )
259 <                                rop->preop.cmat[j*nc+i] *= WHTMELANOPIC;
258 >                        for (i = nc*(cf != 1); i--; )
259 >                                rop->preop.cmat[j*nc+i] *= cf;
260                          break;
261                  case 'A':               /* average component */
262 +                case 'a':
263                          for (i = nc; i--; )
264                                  rop->preop.cmat[j*nc+i] = 1./(double)nc;
265                          break;
# Line 238 | Line 270 | checksymbolic(ROPMAT *rop)
270                  }
271          }
272                                          /* return recommended output type */
273 <        if (!strcmp(rop->preop.csym, "XYZ")) {
273 >        if (!strcasecmp(rop->preop.csym, "XYZ")) {
274                  if (dt <= DTspec)
275                          return(DTxyze);
276 <        } else if (!strcmp(rop->preop.csym, "RGB")) {
276 >        } else if (!strcasecmp(rop->preop.csym, "RGB")) {
277                  if (dt <= DTspec)
278                          return(DTrgbe);
279 <        }
248 <        if ((nc > 3) & (dt <= DTspec))
279 >        } else if (dt == DTspec)
280                  return(DTfloat);        /* probably not actual spectrum */
281          return(0);
282   }
283  
284   /* Get matrix and perform unary operations */
285 < static RMATRIX *
285 > RMATRIX *
286   loadop(ROPMAT *rop)
287   {
288          int     outtype = 0;
# Line 327 | Line 358 | loadop(ROPMAT *rop)
358                  }
359          }
360          if (rop->preop.transpose) {             /* transpose matrix? */
361 <                mres = rmx_transpose(rop->mtx);
331 <                if (mres == NULL) {
361 >                if (!rmx_transpose(rop->mtx)) {
362                          fputs(rop->inspec, stderr);
363                          fputs(": transpose failed\n", stderr);
364                          goto failure;
# Line 337 | Line 367 | loadop(ROPMAT *rop)
367                          fputs(rop->inspec, stderr);
368                          fputs(": transposed rows and columns\n", stderr);
369                  }
340                rmx_free(rop->mtx);
341                rop->mtx = mres;
370          }
371          mres = rop->mtx;
372          rop->mtx = NULL;
# Line 351 | Line 379 | failure:
379   }
380  
381   /* Execute binary operation, free matrix arguments and return new result */
382 < static RMATRIX *
382 > RMATRIX *
383   binaryop(const char *inspec, RMATRIX *mleft, int op, RMATRIX *mright)
384   {
385          RMATRIX *mres = NULL;
# Line 428 | Line 456 | binaryop(const char *inspec, RMATRIX *mleft, int op, R
456   }
457  
458   /* Perform matrix operations from left to right */
459 < static RMATRIX *
459 > RMATRIX *
460   op_left2right(ROPMAT *mop)
461   {
462          RMATRIX *mleft = loadop(mop);
# Line 444 | Line 472 | op_left2right(ROPMAT *mop)
472   }
473  
474   /* Perform matrix operations from right to left */
475 < static RMATRIX *
475 > RMATRIX *
476   op_right2left(ROPMAT *mop)
477   {
478          RMATRIX *mright;
# Line 473 | Line 501 | op_right2left(ROPMAT *mop)
501                                                  : (mop)->mtx->ncols)
502  
503   /* Should we prefer concatenating from rightmost matrix towards left? */
504 < static int
504 > int
505   prefer_right2left(ROPMAT *mop)
506   {
507          int     mri = 0;
# Line 500 | Line 528 | prefer_right2left(ROPMAT *mop)
528          return(t_ncols(mop+mri) < t_nrows(mop));
529   }
530  
531 < static int
531 > int
532   get_factors(double da[], int n, char *av[])
533   {
534          int     ac;
# Line 510 | Line 538 | get_factors(double da[], int n, char *av[])
538          return(ac);
539   }
540  
541 < static ROPMAT *
541 > ROPMAT *
542   resize_moparr(ROPMAT *mop, int n2alloc)
543   {
544          int     nmats = 0;
# Line 518 | Line 546 | resize_moparr(ROPMAT *mop, int n2alloc)
546  
547          while (mop[nmats++].binop)
548                  ;
549 <        for (i = nmats; i > n2alloc; i--)
549 >        for (i = nmats; i >= n2alloc; i--)
550                  rmx_free(mop[i].mtx);
551          mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
552          if (mop == NULL) {
# Line 665 | Line 693 | main(int argc, char *argv[])
693          mres = loadop(mop+nmats);
694          if (mres == NULL)
695                  return(1);
696 <        if (outfmt == DTfromHeader)     /* check data type */
696 >        if ((outfmt == DTfromHeader) & (mres->dtype < DTspec))
697                  outfmt = mres->dtype;
698 <        if (outfmt == DTrgbe) {
698 >        if (outfmt == DTrgbe) {         /* check data type */
699                  if (mres->ncomp > 3)
700                          outfmt = DTspec;
701                  else if (mres->dtype == DTxyze)
702                          outfmt = DTxyze;
703          }
704 + #if DTrmx_native==DTfloat
705 +        if (outfmt == DTdouble)
706 +                fprintf(stderr,
707 +                        "%s: warning - writing float result as double\n",
708 +                                argv[0]);
709 + #endif
710          newheader("RADIANCE", stdout);  /* write result to stdout */
711          printargs(argc, argv, stdout);
712          return(rmx_write(mres, outfmt, stdout) ? 0 : 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines