6 |
|
*/ |
7 |
|
|
8 |
|
#include <errno.h> |
9 |
– |
#include <ctype.h> |
9 |
|
#include "rtio.h" |
10 |
|
#include "resolu.h" |
11 |
|
#include "rmatrix.h" |
15 |
|
|
16 |
|
/* Unary matrix operation(s) */ |
17 |
|
typedef struct { |
19 |
– |
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 |
+ |
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) |
149 |
|
|
150 |
|
for (i = nc; i--; ) { |
151 |
|
SCOLOR sclr; |
152 |
< |
scolorblack(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 |
|
} |
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", |
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? */ |
533 |
|
int |
534 |
|
main(int argc, char *argv[]) |
535 |
|
{ |
536 |
< |
int outfmt = DTfromHeader; |
537 |
< |
int nall = 2; |
538 |
< |
ROPMAT *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT)); |
539 |
< |
int nmats = 0; |
540 |
< |
RMATRIX *mres = NULL; |
541 |
< |
int stdin_used = 0; |
542 |
< |
int i; |
536 |
> |
int outfmt = DTfromHeader; |
537 |
> |
const char *defCsym = NULL; |
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; |
544 |
|
/* get options and arguments */ |
545 |
|
for (i = 1; i < argc; i++) { |
546 |
|
if (argv[i][0] && !argv[i][1] && |
563 |
|
mop[nmats].inspec = stdin_name; |
564 |
|
} else |
565 |
|
mop[nmats].inspec = argv[i]; |
566 |
+ |
if (!mop[nmats].preop.csym) |
567 |
+ |
mop[nmats].preop.csym = defCsym; |
568 |
|
if (nmats > 0 && !mop[nmats-1].binop) |
569 |
|
mop[nmats-1].binop = '.'; |
570 |
|
nmats++; |
606 |
|
goto userr; |
607 |
|
} |
608 |
|
break; |
609 |
+ |
case 'C': |
610 |
+ |
if (!n || isflt(argv[i+1])) |
611 |
+ |
goto userr; |
612 |
+ |
defCsym = mop[nmats].preop.csym = argv[++i]; |
613 |
+ |
mop[nmats].preop.clen = 0; |
614 |
+ |
break; |
615 |
|
case 'c': |
616 |
< |
if (n && isupper(argv[i+1][0])) { |
617 |
< |
strlcpy(mop[nmats].preop.csym, |
547 |
< |
argv[++i], |
548 |
< |
sizeof(mop[0].preop.csym)); |
616 |
> |
if (n && !isflt(argv[i+1])) { |
617 |
> |
mop[nmats].preop.csym = argv[++i]; |
618 |
|
mop[nmats].preop.clen = 0; |
619 |
|
break; |
620 |
|
} |
627 |
|
argv[0]); |
628 |
|
goto userr; |
629 |
|
} |
630 |
< |
mop[nmats].preop.csym[0] = '\0'; |
630 |
> |
mop[nmats].preop.csym = NULL; |
631 |
|
break; |
632 |
|
case 'r': |
633 |
|
if (argv[i][2] == 'f') |