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.13 by greg, Mon Aug 12 01:20:26 2019 UTC vs.
Revision 2.33 by greg, Thu May 16 18:59:19 2024 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * General component matrix operations.
6   */
7  
8 #include <stdio.h>
9 #include <stdlib.h>
8   #include <errno.h>
9   #include "rtio.h"
12 #include "resolu.h"
10   #include "rmatrix.h"
11   #include "platform.h"
12  
13 < #define MAXCOMP         16              /* #components we support */
13 > #define MAXCOMP         MAXCSAMP        /* #components we support */
14  
15 < static const char       stdin_name[] = "<stdin>";
19 <
20 < /* unary matrix operation(s) */
15 > /* Unary matrix operation(s) */
16   typedef struct {
22        double          sca[MAXCOMP];           /* scalar coefficients */
17          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
18 <        short           nsf;                    /* number of scalars */
18 >        double          sca[MAXCOMP];           /* scalar coefficients */
19 >        const char      *csym;                  /* symbolic coefs or file */
20          short           clen;                   /* number of coefficients */
21 +        short           nsf;                    /* number of scalars */
22          short           transpose;              /* do transpose? */
23   } RUNARYOP;
24  
25 < /* matrix input source and requested operation(s) */
25 > /* Matrix input source and requested operation(s) */
26   typedef struct {
27          const char      *inspec;                /* input specification */
28 +        RMPref          rmp;                    /* matrix preference */
29          RUNARYOP        preop;                  /* unary operation(s) */
30          RMATRIX         *mtx;                   /* original matrix if loaded */
31          int             binop;                  /* binary op with next (or 0) */
# Line 40 | Line 37 | int    verbose = 0;                    /* verbose reporting? */
37   static int
38   loadmatrix(ROPMAT *rop)
39   {
40 <        if (rop->mtx != NULL)
40 >        if (rop->mtx != NULL)           /* already loaded? */
41                  return(0);
42  
43 <        rop->mtx = rmx_load(rop->inspec == stdin_name ?
44 <                                (const char *)NULL : rop->inspec);
45 <        if (rop->mtx == NULL) {
46 <                fputs(rop->inspec, stderr);
47 <                fputs(": cannot load matrix\n", stderr);
43 >        rop->mtx = rmx_load(rop->inspec, rop->rmp);
44 >
45 >        return(!rop->mtx ? -1 : 1);
46 > }
47 >
48 > static int      checksymbolic(ROPMAT *rop);
49 >
50 > /* Check/set transform based on a reference input file */
51 > static int
52 > checkreffile(ROPMAT *rop)
53 > {
54 >        static const char       *curRF = NULL;
55 >        static RMATRIX          refm;
56 >        const int               nc = rop->mtx->ncomp;
57 >        int                     i;
58 >
59 >        if (!curRF || strcmp(rop->preop.csym, curRF)) {
60 >                FILE    *fp = fopen(rop->preop.csym, "rb");
61 >                if (!rmx_load_header(&refm, fp)) {
62 >                        fprintf(stderr, "%s: cannot read info header\n",
63 >                                        rop->preop.csym);
64 >                        curRF = NULL;
65 >                        if (fp) fclose(fp);
66 >                        return(-1);
67 >                }
68 >                fclose(fp);
69 >                curRF = rop->preop.csym;
70 >        }
71 >        if (refm.ncomp == 3) {
72 >                rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB";
73 >                return(checksymbolic(rop));
74 >        }
75 >        if (refm.ncomp == 2) {
76 >                fprintf(stderr, "%s: cannot convert to 2 components\n",
77 >                                curRF);
78                  return(-1);
79          }
80 <        return(1);
80 >        if (refm.ncomp == 1) {
81 >                rop->preop.csym = "Y";          /* XXX big assumption */
82 >                return(checksymbolic(rop));
83 >        }
84 >        if (refm.ncomp == nc &&
85 >                        !memcmp(refm.wlpart, rop->mtx->wlpart, sizeof(refm.wlpart)))
86 >                return(0);                      /* nothing to do */
87 >
88 >        if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) {
89 >                fprintf(stderr, "%s: cannot resample from %d to %d components\n",
90 >                                curRF, nc, refm.ncomp);
91 >                return(-1);
92 >        }
93 >        rop->preop.clen = refm.ncomp * nc;      /* compute spec to ref */
94 >
95 >        for (i = 0; i < nc; i++) {
96 >                SCOLOR  scstim, scresp;
97 >                int     j;
98 >                memset(scstim, 0, sizeof(COLORV)*nc);
99 >                scstim[i] = 1.f;
100 >                convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3],
101 >                                scstim, nc, rop->mtx->wlpart[0], rop->mtx->wlpart[3]);
102 >                for (j = refm.ncomp; j-- > 0; )
103 >                        rop->preop.cmat[j*nc + i] = scresp[j];
104 >        }
105 >        memcpy(rop->mtx->wlpart, refm.wlpart, sizeof(rop->mtx->wlpart));
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)
112 + {
113 +        const int       nc = rop->mtx->ncomp;
114 +        const float *   wlp = rop->mtx->wlpart;
115 +        int             i;
116 +
117 +        for (i = nc; i--; ) {
118 +                int     nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
119 +                int     nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
120 +                COLOR   crgb;
121 +                spec_rgb(crgb, nmStart, nmEnd);
122 +                rop->preop.cmat[r*nc+i] = crgb[p];
123 +        }
124 + }
125 +
126 + /* Compute conversion row from spectrum to one channel of XYZ */
127 + static void
128 + xyzrow(ROPMAT *rop, int r, int p)
129 + {
130 +        const int       nc = rop->mtx->ncomp;
131 +        const float *   wlp = rop->mtx->wlpart;
132 +        int             i;
133 +
134 +        for (i = nc; i--; ) {
135 +                int     nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
136 +                int     nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
137 +                COLOR   cxyz;
138 +                spec_cie(cxyz, nmStart, nmEnd);
139 +                rop->preop.cmat[r*nc+i] = cxyz[p];
140 +        }
141 + }
142 +
143 + /* Use the spectral sensitivity function to compute matrix coefficients */
144 + static void
145 + sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
146 + {
147 +        const int       nc = rop->mtx->ncomp;
148 +        int             i;
149 +
150 +        for (i = nc; i--; ) {
151 +                SCOLOR  sclr;
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 + }
157 +
158 + /* Check/set symbolic transform */
159 + static int
160 + checksymbolic(ROPMAT *rop)
161 + {
162 +        const int       nc = rop->mtx->ncomp;
163 +        const int       dt = rop->mtx->dtype;
164 +        double          cf = 1;
165 +        int             i, j;
166 +                                        /* check suffix => reference file */
167 +        if (strchr(rop->preop.csym, '.') > rop->preop.csym)
168 +                return(checkreffile(rop));
169 +
170 +        if (nc < 3) {
171 +                fprintf(stderr, "%s: -c '%s' requires at least 3 components\n",
172 +                                rop->inspec, rop->preop.csym);
173 +                return(-1);
174 +        }
175 +        rop->preop.clen = strlen(rop->preop.csym) * nc;
176 +        if (rop->preop.clen > MAXCOMP*MAXCOMP) {
177 +                fprintf(stderr, "%s: -c '%s' results in too many components\n",
178 +                                rop->inspec, rop->preop.csym);
179 +                return(-1);
180 +        }
181 +        for (j = 0; rop->preop.csym[j]; j++) {
182 +                int     comp = 0;
183 +                switch (rop->preop.csym[j]) {
184 +                case 'B':
185 +                case 'b':
186 +                        ++comp;
187 +                        /* fall through */
188 +                case 'G':
189 +                case 'g':
190 +                        ++comp;
191 +                        /* fall through */
192 +                case 'R':
193 +                case 'r':
194 +                        if (rop->preop.csym[j] <= 'Z')
195 +                                cf = 1./WHTEFFICACY;
196 +                        if (dt == DTxyze) {
197 +                                for (i = 3; i--; )
198 +                                        rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i];
199 +                        } else if (nc == 3)
200 +                                rop->preop.cmat[j*nc+comp] = 1.;
201 +                        else
202 +                                rgbrow(rop, j, comp);
203 +                        break;
204 +                case 'Z':
205 +                case 'z':
206 +                        ++comp;
207 +                        /* fall through */
208 +                case 'Y':
209 +                case 'y':
210 +                        ++comp;
211 +                        /* fall through */
212 +                case 'X':
213 +                case 'x':
214 +                        if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze))
215 +                                cf = WHTEFFICACY;
216 +                        if (dt == DTxyze) {
217 +                                rop->preop.cmat[j*nc+comp] = 1.;
218 +                        } else if (nc == 3) {
219 +                                for (i = 3; i--; )
220 +                                        rop->preop.cmat[j*nc+i] =
221 +                                                        rgb2xyzmat[comp][i];
222 +                        } else if (comp == CIEY)
223 +                                sensrow(rop, j, scolor2photopic);
224 +                        else
225 +                                xyzrow(rop, j, comp);
226 +
227 +                        for (i = nc*(cf != 1); i--; )
228 +                                rop->preop.cmat[j*nc+i] *= cf;
229 +                        break;
230 +                case 'S':               /* scotopic (il)luminance */
231 +                        cf = WHTSCOTOPIC;
232 +                        /* fall through */
233 +                case 's':
234 +                        sensrow(rop, j, scolor2scotopic);
235 +                        for (i = nc*(cf != 1); i--; )
236 +                                rop->preop.cmat[j*nc+i] *= cf;
237 +                        break;
238 +                case 'M':               /* melanopic (il)luminance */
239 +                        cf = WHTMELANOPIC;
240 +                        /* fall through */
241 +                case 'm':
242 +                        sensrow(rop, j, scolor2melanopic);
243 +                        for (i = nc*(cf != 1); i--; )
244 +                                rop->preop.cmat[j*nc+i] *= cf;
245 +                        break;
246 +                case 'A':               /* average component */
247 +                case 'a':
248 +                        for (i = nc; i--; )
249 +                                rop->preop.cmat[j*nc+i] = 1./(double)nc;
250 +                        break;
251 +                default:
252 +                        fprintf(stderr, "%s: -c '%c' unsupported\n",
253 +                                rop->inspec, rop->preop.csym[j]);
254 +                        return(-1);
255 +                }
256 +        }
257 +                                        /* return recommended output type */
258 +        if (!strcasecmp(rop->preop.csym, "XYZ")) {
259 +                if (dt <= DTspec)
260 +                        return(DTxyze);
261 +        } else if (!strcasecmp(rop->preop.csym, "RGB")) {
262 +                if (dt <= DTspec)
263 +                        return(DTrgbe);
264 +        } else if (dt == DTspec)
265 +                return(DTfloat);        /* probably not actual spectrum */
266 +        return(0);
267 + }
268 +
269   /* Get matrix and perform unary operations */
270   static RMATRIX *
271   loadop(ROPMAT *rop)
272   {
273 +        int     outtype = 0;
274          RMATRIX *mres;
275 <        int     i;
275 >        int     i, j;
276  
277          if (loadmatrix(rop) < 0)                /* make sure we're loaded */
278                  return(NULL);
279  
280 <        if (rop->preop.nsf > 0) {               /* apply scalar(s) */
281 <                if (rop->preop.clen > 0) {
282 <                        fputs("Options -s and -c are exclusive\n", stderr);
280 >        if (rop->preop.csym &&                  /* symbolic transform? */
281 >                        (outtype = checksymbolic(rop)) < 0)
282 >                goto failure;
283 >        if (rop->preop.clen > 0) {              /* apply component transform? */
284 >                if (rop->preop.clen % rop->mtx->ncomp) {
285 >                        fprintf(stderr, "%s: -c must have N x %d coefficients\n",
286 >                                        rop->inspec, rop->mtx->ncomp);
287                          goto failure;
288                  }
289 +                if (rop->preop.nsf > 0) {       /* scale transform, first */
290 +                        if (rop->preop.nsf == 1) {
291 +                                for (i = rop->preop.clen; i--; )
292 +                                        rop->preop.cmat[i] *= rop->preop.sca[0];
293 +                        } else if (rop->preop.nsf*rop->mtx->ncomp != rop->preop.clen) {
294 +                                fprintf(stderr, "%s: -s must have one or %d factors\n",
295 +                                                rop->inspec,
296 +                                                rop->preop.clen/rop->mtx->ncomp);
297 +                                goto failure;
298 +                        } else {
299 +                                for (i = rop->preop.nsf; i--; )
300 +                                        for (j = rop->mtx->ncomp; j--; )
301 +                                                rop->preop.cmat[i*rop->mtx->ncomp+j]
302 +                                                                *= rop->preop.sca[i];
303 +                        }
304 +                }
305 +                mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp,
306 +                                        rop->preop.cmat);
307 +                if (mres == NULL) {
308 +                        fprintf(stderr, "%s: matrix transform failed\n",
309 +                                                rop->inspec);
310 +                        goto failure;
311 +                }
312 +                if (verbose)
313 +                        fprintf(stderr, "%s: applied %d x %d transform%s\n",
314 +                                        rop->inspec, mres->ncomp,
315 +                                        rop->mtx->ncomp,
316 +                                        rop->preop.nsf ? " (* scalar)" : "");
317 +                rop->preop.nsf = 0;             /* now folded in */
318 +                if ((mres->ncomp > 3) & (mres->dtype <= DTspec))
319 +                        outtype = DTfloat;      /* probably not actual spectrum */
320 +                rmx_free(rop->mtx);
321 +                rop->mtx = mres;
322 +        }
323 +        if (rop->preop.nsf > 0) {               /* apply scalar(s)? */
324                  if (rop->preop.nsf == 1) {
325                          for (i = rop->mtx->ncomp; --i; )
326                                  rop->preop.sca[i] = rop->preop.sca[0];
# Line 89 | Line 342 | loadop(ROPMAT *rop)
342                          fputs(" )\n", stderr);
343                  }
344          }
345 <        if (rop->preop.clen > 0) {      /* apply transform */
93 <                if (rop->preop.clen % rop->mtx->ncomp) {
94 <                        fprintf(stderr, "%s: -c must have N x %d coefficients\n",
95 <                                        rop->inspec, rop->mtx->ncomp);
96 <                        goto failure;
97 <                }
98 <                mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp,
99 <                                        rop->preop.cmat);
100 <                if (mres == NULL) {
101 <                        fprintf(stderr, "%s: matrix transform failed\n",
102 <                                                rop->inspec);
103 <                        goto failure;
104 <                }
105 <                if (verbose)
106 <                        fprintf(stderr, "%s: applied %d x %d transform\n",
107 <                                        rop->inspec, mres->ncomp,
108 <                                        rop->mtx->ncomp);
109 <                rmx_free(rop->mtx);
110 <                rop->mtx = mres;
111 <        }
112 <        if (rop->preop.transpose) {     /* transpose matrix? */
345 >        if (rop->preop.transpose) {             /* transpose matrix? */
346                  mres = rmx_transpose(rop->mtx);
347                  if (mres == NULL) {
348                          fputs(rop->inspec, stderr);
# Line 125 | Line 358 | loadop(ROPMAT *rop)
358          }
359          mres = rop->mtx;
360          rop->mtx = NULL;
361 +        if (outtype)
362 +                mres->dtype = outtype;
363          return(mres);
364   failure:
365          rmx_free(rop->mtx);
# Line 140 | Line 375 | binaryop(const char *inspec, RMATRIX *mleft, int op, R
375  
376          if ((mleft == NULL) | (mright == NULL))
377                  return(NULL);
143
378          switch (op) {
379          case '.':                       /* concatenate */
380 <                mres = rmx_multiply(mleft, mright);
380 >                if (mleft->ncomp != mright->ncomp) {
381 >                        fputs(inspec, stderr);
382 >                        fputs(": # components do not match\n", stderr);
383 >                } else if (mleft->ncols != mright->nrows) {
384 >                        fputs(inspec, stderr);
385 >                        fputs(": mismatched dimensions\n",
386 >                                        stderr);
387 >                } else
388 >                        mres = rmx_multiply(mleft, mright);
389                  rmx_free(mleft);
390                  rmx_free(mright);
391                  if (mres == NULL) {
392                          fputs(inspec, stderr);
393 <                        if (mleft->ncols != mright->nrows)
152 <                                fputs(": mismatched dimensions for multiply\n",
153 <                                                stderr);
154 <                        else
155 <                                fputs(": concatenation failed\n", stderr);
393 >                        fputs(": concatenation failed\n", stderr);
394                          return(NULL);
395                  }
396                  if (verbose) {
# Line 229 | Line 467 | op_right2left(ROPMAT *mop)
467          int     rpos = 0;
468                                          /* find end of list */
469          while (mop[rpos].binop)
470 <                rpos++;
470 >                if (mop[rpos++].binop != '.') {
471 >                        fputs(
472 >                "Right-to-left evaluation only for matrix multiplication!\n",
473 >                                        stderr);
474 >                        return(NULL);
475 >                }
476          mright = loadop(mop+rpos);
477          while (rpos-- > 0) {
478                  if (mright == NULL)
479                          break;
480 <                mright = binaryop(mop[rpos].inspec,
480 >                mright = binaryop(mop[rpos+1].inspec,
481                                  loadop(mop+rpos), mop[rpos].binop, mright);
482          }
483          return(mright);
# Line 284 | Line 527 | get_factors(double da[], int n, char *av[])
527   }
528  
529   static ROPMAT *
530 < grow_moparray(ROPMAT *mop, int n2alloc)
530 > resize_moparr(ROPMAT *mop, int n2alloc)
531   {
532          int     nmats = 0;
533 +        int     i;
534  
535          while (mop[nmats++].binop)
536                  ;
537 +        for (i = nmats; i > n2alloc; i--)
538 +                rmx_free(mop[i].mtx);
539          mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
540          if (mop == NULL) {
541 <                fputs("Out of memory in grow_moparray()\n", stderr);
541 >                fputs("Out of memory in resize_moparr()\n", stderr);
542                  exit(1);
543          }
544          if (n2alloc > nmats)
# Line 304 | Line 550 | grow_moparray(ROPMAT *mop, int n2alloc)
550   int
551   main(int argc, char *argv[])
552   {
553 <        int     outfmt = DTfromHeader;
554 <        int     nall = 2;
555 <        ROPMAT  *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
556 <        int     nmats = 0;
557 <        RMATRIX *mres = NULL;
558 <        int     stdin_used = 0;
559 <        int     i;
553 >        int             outfmt = DTfromHeader;
554 >        const char      *defCsym = NULL;
555 >        int             nall = 2;
556 >        ROPMAT          *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
557 >        int             nmats = 0;
558 >        RMATRIX         *mres = NULL;
559 >        int             stdin_used = 0;
560 >        int             i;
561                                          /* get options and arguments */
562          for (i = 1; i < argc; i++) {
316                if (nmats >= nall)
317                        mop = grow_moparray(mop, nall += 2);
563                  if (argv[i][0] && !argv[i][1] &&
564                                  strchr(".+*/", argv[i][0]) != NULL) {
565 <                        if (mop[nmats].inspec == NULL || mop[nmats].binop) {
566 <                                fprintf(stderr, "%s: missing matrix argument\n",
567 <                                                argv[0]);
565 >                        if (!nmats || mop[nmats-1].binop) {
566 >                                fprintf(stderr,
567 >                        "%s: missing matrix argument before '%c' operation\n",
568 >                                                argv[0], argv[i][0]);
569                                  return(1);
570                          }
571 <                        mop[nmats++].binop = argv[i][0];
571 >                        mop[nmats-1].binop = argv[i][0];
572                  } else if (argv[i][0] != '-' || !argv[i][1]) {
573                          if (argv[i][0] == '-') {
574                                  if (stdin_used++) {
# Line 334 | Line 580 | main(int argc, char *argv[])
580                                  mop[nmats].inspec = stdin_name;
581                          } else
582                                  mop[nmats].inspec = argv[i];
583 +                        if (!mop[nmats].preop.csym)
584 +                                mop[nmats].preop.csym = defCsym;
585                          if (nmats > 0 && !mop[nmats-1].binop)
586                                  mop[nmats-1].binop = '.';
587                          nmats++;
# Line 341 | Line 589 | main(int argc, char *argv[])
589                          int     n = argc-1 - i;
590                          switch (argv[i][1]) {   /* get option */
591                          case 'v':
592 <                                verbose = !verbose;
592 >                                verbose++;
593                                  break;
594                          case 'f':
595                                  switch (argv[i][2]) {
# Line 369 | Line 617 | main(int argc, char *argv[])
617                                  i += mop[nmats].preop.nsf =
618                                          get_factors(mop[nmats].preop.sca,
619                                                          n, argv+i+1);
620 +                                if (mop[nmats].preop.nsf <= 0) {
621 +                                        fprintf(stderr, "%s: -s missing arguments\n",
622 +                                                        argv[0]);
623 +                                        goto userr;
624 +                                }
625                                  break;
626 +                        case 'C':
627 +                                if (!n || isflt(argv[i+1]))
628 +                                        goto userr;
629 +                                defCsym = mop[nmats].preop.csym = argv[++i];
630 +                                mop[nmats].preop.clen = 0;
631 +                                break;
632                          case 'c':
633 +                                if (n && !isflt(argv[i+1])) {
634 +                                        mop[nmats].preop.csym = argv[++i];
635 +                                        mop[nmats].preop.clen = 0;
636 +                                        break;
637 +                                }
638                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
639                                  i += mop[nmats].preop.clen =
640                                          get_factors(mop[nmats].preop.cmat,
641                                                          n, argv+i+1);
642 +                                if (mop[nmats].preop.clen <= 0) {
643 +                                        fprintf(stderr, "%s: -c missing arguments\n",
644 +                                                        argv[0]);
645 +                                        goto userr;
646 +                                }
647 +                                mop[nmats].preop.csym = NULL;
648                                  break;
649 +                        case 'r':
650 +                                if (argv[i][2] == 'f')
651 +                                        mop[nmats].rmp = RMPreflF;
652 +                                else if (argv[i][2] == 'b')
653 +                                        mop[nmats].rmp = RMPreflB;
654 +                                else
655 +                                        goto userr;
656 +                                break;
657                          default:
658                                  fprintf(stderr, "%s: unknown operation '%s'\n",
659                                                  argv[0], argv[i]);
660                                  goto userr;
661                          }
662                  }
663 +                if (nmats >= nall)
664 +                        mop = resize_moparr(mop, nall += 2);
665          }
666          if (mop[0].inspec == NULL)      /* nothing to do? */
667                  goto userr;
668 +        if (mop[nmats-1].binop) {
669 +                fprintf(stderr,
670 +                        "%s: missing matrix argument after '%c' operation\n",
671 +                                argv[0], mop[nmats-1].binop);
672 +                return(1);
673 +        }
674                                          /* favor quicker concatenation */
675 <        mres = prefer_right2left(mop) ? op_right2left(mop) : op_left2right(mop);
676 <        if (!mres)
675 >        mop[nmats].mtx = prefer_right2left(mop) ? op_right2left(mop)
676 >                                                : op_left2right(mop);
677 >        if (mop[nmats].mtx == NULL)
678                  return(1);
679 <                                        /* write result to stdout */
680 <        if (outfmt == DTfromHeader)
681 <                outfmt = mres->dtype;
682 <        if (outfmt != DTascii)
396 <                SET_FILE_BINARY(stdout);
397 <        newheader("RADIANCE", stdout);
398 <        printargs(argc, argv, stdout);
399 <        if (!rmx_write(mres, outfmt, stdout)) {
400 <                fprintf(stderr, "%s: error writing result matrix\n", argv[0]);
679 >                                        /* apply trailing unary operations */
680 >        mop[nmats].inspec = "trailing_ops";
681 >        mres = loadop(mop+nmats);
682 >        if (mres == NULL)
683                  return(1);
684 +        if (outfmt == DTfromHeader)     /* check data type */
685 +                outfmt = mres->dtype;
686 +        if (outfmt == DTrgbe) {
687 +                if (mres->ncomp > 3)
688 +                        outfmt = DTspec;
689 +                else if (mres->dtype == DTxyze)
690 +                        outfmt = DTxyze;
691          }
692 <        /* rmx_free(mres); free(mop); */
693 <        return(0);
692 >        newheader("RADIANCE", stdout);  /* write result to stdout */
693 >        printargs(argc, argv, stdout);
694 >        return(rmx_write(mres, outfmt, stdout) ? 0 : 1);
695   userr:
696          fprintf(stderr,
697 <        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [.+*/] .. > mres\n",
697 >        "Usage: %s [-v][-f{adfc}][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n",
698                          argv[0]);
699          return(1);
700   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines