| 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" |
| 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) */ |
| 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) |
| 150 |
|
|
| 151 |
|
for (i = nc; i--; ) { |
| 152 |
|
SCOLOR sclr; |
| 153 |
< |
scolorblack(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 |
|
} |
| 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", |
| 230 |
|
break; |
| 231 |
|
case 'A': /* average component */ |
| 232 |
|
for (i = nc; i--; ) |
| 233 |
< |
rop->preop->cmat[j*nc+i] = 1./(double)nc; |
| 233 |
> |
rop->preop.cmat[j*nc+i] = 1./(double)nc; |
| 234 |
|
break; |
| 235 |
|
default: |
| 236 |
|
fprintf(stderr, "%s: -c '%c' unsupported\n", |
| 262 |
|
if (loadmatrix(rop) < 0) /* make sure we're loaded */ |
| 263 |
|
return(NULL); |
| 264 |
|
|
| 265 |
< |
if (rop->preop.csym[0] && /* symbolic transform? */ |
| 265 |
> |
if (rop->preop.csym && /* symbolic transform? */ |
| 266 |
|
(outtype = checksymbolic(rop)) < 0) |
| 267 |
|
goto failure; |
| 268 |
|
if (rop->preop.clen > 0) { /* apply component transform? */ |
| 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) { |
| 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, rop->mtx->ncomp); |
| 280 |
> |
rop->inspec, |
| 281 |
> |
rop->preop.clen/rop->mtx->ncomp); |
| 282 |
|
goto failure; |
| 283 |
|
} else { |
| 284 |
< |
for (j = rop->preop.clen/rop->preop.nsf; j--; ) |
| 285 |
< |
for (i = rop->preop.nsf; i--; ) |
| 286 |
< |
rop->preop.cmat[j*rop->preop.nsf+i] *= |
| 287 |
< |
rop->preop.sca[i]; |
| 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, |
| 299 |
|
rop->inspec, mres->ncomp, |
| 300 |
|
rop->mtx->ncomp, |
| 301 |
|
rop->preop.nsf ? " (* scalar)" : ""); |
| 302 |
< |
rop->preop.nsf = 0; |
| 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); |
| 512 |
|
} |
| 513 |
|
|
| 514 |
|
static ROPMAT * |
| 515 |
< |
grow_moparray(ROPMAT *mop, int n2alloc) |
| 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 grow_moparray()\n", stderr); |
| 526 |
> |
fputs("Out of memory in resize_moparr()\n", stderr); |
| 527 |
|
exit(1); |
| 528 |
|
} |
| 529 |
|
if (n2alloc > nmats) |
| 535 |
|
int |
| 536 |
|
main(int argc, char *argv[]) |
| 537 |
|
{ |
| 538 |
< |
int outfmt = DTfromHeader; |
| 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; |
| 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] && |
| 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++; |
| 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 && isupper(argv[i+1][0])) { |
| 619 |
< |
strlcpy(mop[nmats].preop.csym, |
| 545 |
< |
argv[++i], |
| 546 |
< |
sizeof(mop[0].preop.csym)); |
| 618 |
> |
if (n && !isflt(argv[i+1])) { |
| 619 |
> |
mop[nmats].preop.csym = argv[++i]; |
| 620 |
|
mop[nmats].preop.clen = 0; |
| 621 |
|
break; |
| 622 |
|
} |
| 629 |
|
argv[0]); |
| 630 |
|
goto userr; |
| 631 |
|
} |
| 632 |
< |
mop[nmats].preop.csym[0] = '\0'; |
| 632 |
> |
mop[nmats].preop.csym = NULL; |
| 633 |
|
break; |
| 634 |
|
case 'r': |
| 635 |
|
if (argv[i][2] == 'f') |
| 646 |
|
} |
| 647 |
|
} |
| 648 |
|
if (nmats >= nall) |
| 649 |
< |
mop = grow_moparray(mop, nall += 2); |
| 649 |
> |
mop = resize_moparr(mop, nall += 2); |
| 650 |
|
} |
| 651 |
|
if (mop[0].inspec == NULL) /* nothing to do? */ |
| 652 |
|
goto userr; |
| 674 |
|
else if (mres->dtype == DTxyze) |
| 675 |
|
outfmt = DTxyze; |
| 676 |
|
} |
| 677 |
< |
if (outfmt != DTascii) /* write result to stdout */ |
| 605 |
< |
SET_FILE_BINARY(stdout); |
| 606 |
< |
newheader("RADIANCE", stdout); |
| 677 |
> |
newheader("RADIANCE", stdout); /* write result to stdout */ |
| 678 |
|
printargs(argc, argv, stdout); |
| 608 |
– |
|
| 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", |
| 682 |
> |
"Usage: %s [-v][-f{adfc}][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n", |
| 683 |
|
argv[0]); |
| 684 |
|
return(1); |
| 685 |
|
} |