ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmtxop.c
Revision: 2.29
Committed: Sun Dec 3 03:44:42 2023 UTC (4 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +2 -1 lines
Log Message:
fix(rmtxop): Corrected wavelength split output based on reference input

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rmtxop.c,v 2.28 2023/12/03 02:28:33 greg Exp $";
3 #endif
4 /*
5 * General component matrix operations.
6 */
7
8 #include <errno.h>
9 #include "rtio.h"
10 #include "resolu.h"
11 #include "rmatrix.h"
12 #include "platform.h"
13
14 #define MAXCOMP MAXCSAMP /* #components we support */
15
16 /* Unary matrix operation(s) */
17 typedef struct {
18 double cmat[MAXCOMP*MAXCOMP]; /* component transformation */
19 double sca[MAXCOMP]; /* scalar coefficients */
20 const char *csym; /* symbolic coefs or file */
21 short clen; /* number of coefficients */
22 short nsf; /* number of scalars */
23 short transpose; /* do transpose? */
24 } RUNARYOP;
25
26 /* Matrix input source and requested operation(s) */
27 typedef struct {
28 const char *inspec; /* input specification */
29 RMPref rmp; /* matrix preference */
30 RUNARYOP preop; /* unary operation(s) */
31 RMATRIX *mtx; /* original matrix if loaded */
32 int binop; /* binary op with next (or 0) */
33 } ROPMAT;
34
35 int verbose = 0; /* verbose reporting? */
36
37 /* Load matrix */
38 static int
39 loadmatrix(ROPMAT *rop)
40 {
41 if (rop->mtx != NULL) /* already loaded? */
42 return(0);
43
44 rop->mtx = rmx_load(rop->inspec, rop->rmp);
45
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 memcpy(rop->mtx->wlpart, refm.wlpart, sizeof(rop->mtx->wlpart));
107 return(0);
108 }
109
110 /* Compute conversion row from spectrum to one channel of RGB */
111 static void
112 rgbrow(ROPMAT *rop, int r, int p)
113 {
114 const int nc = rop->mtx->ncomp;
115 const float * wlp = rop->mtx->wlpart;
116 int i;
117
118 for (i = nc; i--; ) {
119 int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
120 int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
121 COLOR crgb;
122 spec_rgb(crgb, nmStart, nmEnd);
123 rop->preop.cmat[r*nc+i] = crgb[p];
124 }
125 }
126
127 /* Compute conversion row from spectrum to one channel of XYZ */
128 static void
129 xyzrow(ROPMAT *rop, int r, int p)
130 {
131 const int nc = rop->mtx->ncomp;
132 const float * wlp = rop->mtx->wlpart;
133 int i;
134
135 for (i = nc; i--; ) {
136 int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
137 int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
138 COLOR cxyz;
139 spec_cie(cxyz, nmStart, nmEnd);
140 rop->preop.cmat[r*nc+i] = cxyz[p];
141 }
142 }
143
144 /* Use the spectral sensitivity function to compute matrix coefficients */
145 static void
146 sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
147 {
148 const int nc = rop->mtx->ncomp;
149 int i;
150
151 for (i = nc; i--; ) {
152 SCOLOR sclr;
153 memset(sclr, 0, sizeof(COLORV)*nc);
154 sclr[i] = 1.f;
155 rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->mtx->wlpart);
156 }
157 }
158
159 /* Check/set symbolic transform */
160 static int
161 checksymbolic(ROPMAT *rop)
162 {
163 const int nc = rop->mtx->ncomp;
164 const int dt = rop->mtx->dtype;
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 ++comp;
186 /* fall through */
187 case 'G':
188 ++comp;
189 /* fall through */
190 case 'R':
191 if (dt == DTxyze) {
192 for (i = 3; i--; )
193 rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY *
194 xyz2rgbmat[comp][i];
195 } else if (nc == 3)
196 rop->preop.cmat[j*nc+comp] = 1.;
197 else
198 rgbrow(rop, j, comp);
199 break;
200 case 'Z':
201 ++comp;
202 /* fall through */
203 case 'Y':
204 ++comp;
205 /* fall through */
206 case 'X':
207 if (dt == DTxyze) {
208 rop->preop.cmat[j*nc+comp] = 1.;
209 } else if (nc == 3) {
210 for (i = 3; i--; )
211 rop->preop.cmat[j*nc+i] =
212 rgb2xyzmat[comp][i];
213 } else if (comp == CIEY)
214 sensrow(rop, j, scolor2photopic);
215 else
216 xyzrow(rop, j, comp);
217
218 for (i = nc*(dt != DTxyze); i--; )
219 rop->preop.cmat[j*nc+i] *= WHTEFFICACY;
220 break;
221 case 'S': /* scotopic (il)luminance */
222 sensrow(rop, j, scolor2scotopic);
223 for (i = nc; i--; )
224 rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC;
225 break;
226 case 'M': /* melanopic (il)luminance */
227 sensrow(rop, j, scolor2melanopic);
228 for (i = nc; i--; )
229 rop->preop.cmat[j*nc+i] *= WHTMELANOPIC;
230 break;
231 case 'A': /* average component */
232 for (i = nc; i--; )
233 rop->preop.cmat[j*nc+i] = 1./(double)nc;
234 break;
235 default:
236 fprintf(stderr, "%s: -c '%c' unsupported\n",
237 rop->inspec, rop->preop.csym[j]);
238 return(-1);
239 }
240 }
241 /* return recommended output type */
242 if (!strcmp(rop->preop.csym, "XYZ")) {
243 if (dt <= DTspec)
244 return(DTxyze);
245 } else if (!strcmp(rop->preop.csym, "RGB")) {
246 if (dt <= DTspec)
247 return(DTrgbe);
248 }
249 if ((nc > 3) & (dt <= DTspec))
250 return(DTfloat); /* probably not actual spectrum */
251 return(0);
252 }
253
254 /* Get matrix and perform unary operations */
255 static RMATRIX *
256 loadop(ROPMAT *rop)
257 {
258 int outtype = 0;
259 RMATRIX *mres;
260 int i, j;
261
262 if (loadmatrix(rop) < 0) /* make sure we're loaded */
263 return(NULL);
264
265 if (rop->preop.csym && /* symbolic transform? */
266 (outtype = checksymbolic(rop)) < 0)
267 goto failure;
268 if (rop->preop.clen > 0) { /* apply component transform? */
269 if (rop->preop.clen % rop->mtx->ncomp) {
270 fprintf(stderr, "%s: -c must have N x %d coefficients\n",
271 rop->inspec, rop->mtx->ncomp);
272 goto failure;
273 }
274 if (rop->preop.nsf > 0) { /* scale transform, first */
275 if (rop->preop.nsf == 1) {
276 for (i = rop->preop.clen; i--; )
277 rop->preop.cmat[i] *= rop->preop.sca[0];
278 } else if (rop->preop.nsf*rop->mtx->ncomp != rop->preop.clen) {
279 fprintf(stderr, "%s: -s must have one or %d factors\n",
280 rop->inspec,
281 rop->preop.clen/rop->mtx->ncomp);
282 goto failure;
283 } else {
284 for (i = rop->preop.nsf; i--; )
285 for (j = rop->mtx->ncomp; j--; )
286 rop->preop.cmat[i*rop->mtx->ncomp+j]
287 *= rop->preop.sca[i];
288 }
289 }
290 mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp,
291 rop->preop.cmat);
292 if (mres == NULL) {
293 fprintf(stderr, "%s: matrix transform failed\n",
294 rop->inspec);
295 goto failure;
296 }
297 if (verbose)
298 fprintf(stderr, "%s: applied %d x %d transform%s\n",
299 rop->inspec, mres->ncomp,
300 rop->mtx->ncomp,
301 rop->preop.nsf ? " (* scalar)" : "");
302 rop->preop.nsf = 0; /* now folded in */
303 if ((mres->ncomp > 3) & (mres->dtype <= DTspec))
304 outtype = DTfloat; /* probably not actual spectrum */
305 rmx_free(rop->mtx);
306 rop->mtx = mres;
307 }
308 if (rop->preop.nsf > 0) { /* apply scalar(s)? */
309 if (rop->preop.nsf == 1) {
310 for (i = rop->mtx->ncomp; --i; )
311 rop->preop.sca[i] = rop->preop.sca[0];
312 } else if (rop->preop.nsf != rop->mtx->ncomp) {
313 fprintf(stderr, "%s: -s must have one or %d factors\n",
314 rop->inspec, rop->mtx->ncomp);
315 goto failure;
316 }
317 if (!rmx_scale(rop->mtx, rop->preop.sca)) {
318 fputs(rop->inspec, stderr);
319 fputs(": scalar operation failed\n", stderr);
320 goto failure;
321 }
322 if (verbose) {
323 fputs(rop->inspec, stderr);
324 fputs(": applied scalar (", stderr);
325 for (i = 0; i < rop->preop.nsf; i++)
326 fprintf(stderr, " %f", rop->preop.sca[i]);
327 fputs(" )\n", stderr);
328 }
329 }
330 if (rop->preop.transpose) { /* transpose matrix? */
331 mres = rmx_transpose(rop->mtx);
332 if (mres == NULL) {
333 fputs(rop->inspec, stderr);
334 fputs(": transpose failed\n", stderr);
335 goto failure;
336 }
337 if (verbose) {
338 fputs(rop->inspec, stderr);
339 fputs(": transposed rows and columns\n", stderr);
340 }
341 rmx_free(rop->mtx);
342 rop->mtx = mres;
343 }
344 mres = rop->mtx;
345 rop->mtx = NULL;
346 if (outtype)
347 mres->dtype = outtype;
348 return(mres);
349 failure:
350 rmx_free(rop->mtx);
351 return(rop->mtx = NULL);
352 }
353
354 /* Execute binary operation, free matrix arguments and return new result */
355 static RMATRIX *
356 binaryop(const char *inspec, RMATRIX *mleft, int op, RMATRIX *mright)
357 {
358 RMATRIX *mres = NULL;
359 int i;
360
361 if ((mleft == NULL) | (mright == NULL))
362 return(NULL);
363 switch (op) {
364 case '.': /* concatenate */
365 if (mleft->ncomp != mright->ncomp) {
366 fputs(inspec, stderr);
367 fputs(": # components do not match\n", stderr);
368 } else if (mleft->ncols != mright->nrows) {
369 fputs(inspec, stderr);
370 fputs(": mismatched dimensions\n",
371 stderr);
372 } else
373 mres = rmx_multiply(mleft, mright);
374 rmx_free(mleft);
375 rmx_free(mright);
376 if (mres == NULL) {
377 fputs(inspec, stderr);
378 fputs(": concatenation failed\n", stderr);
379 return(NULL);
380 }
381 if (verbose) {
382 fputs(inspec, stderr);
383 fputs(": concatenated matrix\n", stderr);
384 }
385 break;
386 case '+':
387 if (!rmx_sum(mleft, mright, NULL)) {
388 fputs(inspec, stderr);
389 fputs(": matrix sum failed\n", stderr);
390 rmx_free(mleft);
391 rmx_free(mright);
392 return(NULL);
393 }
394 if (verbose) {
395 fputs(inspec, stderr);
396 fputs(": added in matrix\n", stderr);
397 }
398 rmx_free(mright);
399 mres = mleft;
400 break;
401 case '*':
402 case '/': {
403 const char * tnam = (op == '/') ?
404 "division" : "multiplication";
405 errno = 0;
406 if (!rmx_elemult(mleft, mright, (op == '/'))) {
407 fprintf(stderr, "%s: element-wise %s failed\n",
408 inspec, tnam);
409 rmx_free(mleft);
410 rmx_free(mright);
411 return(NULL);
412 }
413 if (errno)
414 fprintf(stderr,
415 "%s: warning - error during element-wise %s\n",
416 inspec, tnam);
417 else if (verbose)
418 fprintf(stderr, "%s: element-wise %s\n", inspec, tnam);
419 rmx_free(mright);
420 mres = mleft;
421 } break;
422 default:
423 fprintf(stderr, "%s: unknown operation '%c'\n", inspec, op);
424 rmx_free(mleft);
425 rmx_free(mright);
426 return(NULL);
427 }
428 return(mres);
429 }
430
431 /* Perform matrix operations from left to right */
432 static RMATRIX *
433 op_left2right(ROPMAT *mop)
434 {
435 RMATRIX *mleft = loadop(mop);
436
437 while (mop->binop) {
438 if (mleft == NULL)
439 break;
440 mleft = binaryop(mop[1].inspec,
441 mleft, mop->binop, loadop(mop+1));
442 mop++;
443 }
444 return(mleft);
445 }
446
447 /* Perform matrix operations from right to left */
448 static RMATRIX *
449 op_right2left(ROPMAT *mop)
450 {
451 RMATRIX *mright;
452 int rpos = 0;
453 /* find end of list */
454 while (mop[rpos].binop)
455 if (mop[rpos++].binop != '.') {
456 fputs(
457 "Right-to-left evaluation only for matrix multiplication!\n",
458 stderr);
459 return(NULL);
460 }
461 mright = loadop(mop+rpos);
462 while (rpos-- > 0) {
463 if (mright == NULL)
464 break;
465 mright = binaryop(mop[rpos+1].inspec,
466 loadop(mop+rpos), mop[rpos].binop, mright);
467 }
468 return(mright);
469 }
470
471 #define t_nrows(mop) ((mop)->preop.transpose ? (mop)->mtx->ncols \
472 : (mop)->mtx->nrows)
473 #define t_ncols(mop) ((mop)->preop.transpose ? (mop)->mtx->nrows \
474 : (mop)->mtx->ncols)
475
476 /* Should we prefer concatenating from rightmost matrix towards left? */
477 static int
478 prefer_right2left(ROPMAT *mop)
479 {
480 int mri = 0;
481
482 while (mop[mri].binop) /* find rightmost matrix */
483 if (mop[mri++].binop != '.')
484 return(0); /* pre-empt reversal for other ops */
485
486 if (mri <= 1)
487 return(0); /* won't matter */
488
489 if (loadmatrix(mop+mri) < 0) /* load rightmost cat */
490 return(1); /* fail will bail in a moment */
491
492 if (t_ncols(mop+mri) == 1)
493 return(1); /* definitely better R->L */
494
495 if (t_ncols(mop+mri) >= t_nrows(mop+mri))
496 return(0); /* ...probably worse */
497
498 if (loadmatrix(mop) < 0) /* load leftmost */
499 return(0); /* fail will bail in a moment */
500
501 return(t_ncols(mop+mri) < t_nrows(mop));
502 }
503
504 static int
505 get_factors(double da[], int n, char *av[])
506 {
507 int ac;
508
509 for (ac = 0; ac < n && isflt(av[ac]); ac++)
510 da[ac] = atof(av[ac]);
511 return(ac);
512 }
513
514 static ROPMAT *
515 resize_moparr(ROPMAT *mop, int n2alloc)
516 {
517 int nmats = 0;
518 int i;
519
520 while (mop[nmats++].binop)
521 ;
522 for (i = nmats; i > n2alloc; i--)
523 rmx_free(mop[i].mtx);
524 mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
525 if (mop == NULL) {
526 fputs("Out of memory in resize_moparr()\n", stderr);
527 exit(1);
528 }
529 if (n2alloc > nmats)
530 memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
531 return(mop);
532 }
533
534 /* Load one or more matrices and operate on them, sending results to stdout */
535 int
536 main(int argc, char *argv[])
537 {
538 int outfmt = DTfromHeader;
539 const char *defCsym = NULL;
540 int nall = 2;
541 ROPMAT *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
542 int nmats = 0;
543 RMATRIX *mres = NULL;
544 int stdin_used = 0;
545 int i;
546 /* get options and arguments */
547 for (i = 1; i < argc; i++) {
548 if (argv[i][0] && !argv[i][1] &&
549 strchr(".+*/", argv[i][0]) != NULL) {
550 if (!nmats || mop[nmats-1].binop) {
551 fprintf(stderr,
552 "%s: missing matrix argument before '%c' operation\n",
553 argv[0], argv[i][0]);
554 return(1);
555 }
556 mop[nmats-1].binop = argv[i][0];
557 } else if (argv[i][0] != '-' || !argv[i][1]) {
558 if (argv[i][0] == '-') {
559 if (stdin_used++) {
560 fprintf(stderr,
561 "%s: standard input used for more than one matrix\n",
562 argv[0]);
563 return(1);
564 }
565 mop[nmats].inspec = stdin_name;
566 } else
567 mop[nmats].inspec = argv[i];
568 if (!mop[nmats].preop.csym)
569 mop[nmats].preop.csym = defCsym;
570 if (nmats > 0 && !mop[nmats-1].binop)
571 mop[nmats-1].binop = '.';
572 nmats++;
573 } else {
574 int n = argc-1 - i;
575 switch (argv[i][1]) { /* get option */
576 case 'v':
577 verbose++;
578 break;
579 case 'f':
580 switch (argv[i][2]) {
581 case 'd':
582 outfmt = DTdouble;
583 break;
584 case 'f':
585 outfmt = DTfloat;
586 break;
587 case 'a':
588 outfmt = DTascii;
589 break;
590 case 'c':
591 outfmt = DTrgbe;
592 break;
593 default:
594 goto userr;
595 }
596 break;
597 case 't':
598 mop[nmats].preop.transpose = 1;
599 break;
600 case 's':
601 if (n > MAXCOMP) n = MAXCOMP;
602 i += mop[nmats].preop.nsf =
603 get_factors(mop[nmats].preop.sca,
604 n, argv+i+1);
605 if (mop[nmats].preop.nsf <= 0) {
606 fprintf(stderr, "%s: -s missing arguments\n",
607 argv[0]);
608 goto userr;
609 }
610 break;
611 case 'C':
612 if (!n || isflt(argv[i+1]))
613 goto userr;
614 defCsym = mop[nmats].preop.csym = argv[++i];
615 mop[nmats].preop.clen = 0;
616 break;
617 case 'c':
618 if (n && !isflt(argv[i+1])) {
619 mop[nmats].preop.csym = argv[++i];
620 mop[nmats].preop.clen = 0;
621 break;
622 }
623 if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
624 i += mop[nmats].preop.clen =
625 get_factors(mop[nmats].preop.cmat,
626 n, argv+i+1);
627 if (mop[nmats].preop.clen <= 0) {
628 fprintf(stderr, "%s: -c missing arguments\n",
629 argv[0]);
630 goto userr;
631 }
632 mop[nmats].preop.csym = NULL;
633 break;
634 case 'r':
635 if (argv[i][2] == 'f')
636 mop[nmats].rmp = RMPreflF;
637 else if (argv[i][2] == 'b')
638 mop[nmats].rmp = RMPreflB;
639 else
640 goto userr;
641 break;
642 default:
643 fprintf(stderr, "%s: unknown operation '%s'\n",
644 argv[0], argv[i]);
645 goto userr;
646 }
647 }
648 if (nmats >= nall)
649 mop = resize_moparr(mop, nall += 2);
650 }
651 if (mop[0].inspec == NULL) /* nothing to do? */
652 goto userr;
653 if (mop[nmats-1].binop) {
654 fprintf(stderr,
655 "%s: missing matrix argument after '%c' operation\n",
656 argv[0], mop[nmats-1].binop);
657 return(1);
658 }
659 /* favor quicker concatenation */
660 mop[nmats].mtx = prefer_right2left(mop) ? op_right2left(mop)
661 : op_left2right(mop);
662 if (mop[nmats].mtx == NULL)
663 return(1);
664 /* apply trailing unary operations */
665 mop[nmats].inspec = "trailing_ops";
666 mres = loadop(mop+nmats);
667 if (mres == NULL)
668 return(1);
669 if (outfmt == DTfromHeader) /* check data type */
670 outfmt = mres->dtype;
671 if (outfmt == DTrgbe) {
672 if (mres->ncomp > 3)
673 outfmt = DTspec;
674 else if (mres->dtype == DTxyze)
675 outfmt = DTxyze;
676 }
677 newheader("RADIANCE", stdout); /* write result to stdout */
678 printargs(argc, argv, stdout);
679 return(rmx_write(mres, outfmt, stdout) ? 0 : 1);
680 userr:
681 fprintf(stderr,
682 "Usage: %s [-v][-f{adfc}][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n",
683 argv[0]);
684 return(1);
685 }