6 |
|
*/ |
7 |
|
|
8 |
|
#include <errno.h> |
9 |
– |
#include <ctype.h> |
9 |
|
#include "rtio.h" |
11 |
– |
#include "resolu.h" |
10 |
|
#include "rmatrix.h" |
11 |
|
#include "platform.h" |
12 |
|
|
15 |
– |
#define MAXCOMP MAXCSAMP /* #components we support */ |
16 |
– |
|
13 |
|
/* Unary matrix operation(s) */ |
14 |
|
typedef struct { |
19 |
– |
double sca[MAXCOMP]; /* scalar coefficients */ |
15 |
|
double cmat[MAXCOMP*MAXCOMP]; /* component transformation */ |
16 |
< |
short nsf; /* number of scalars */ |
16 |
> |
double sca[MAXCOMP]; /* scalar coefficients */ |
17 |
> |
const char *csym; /* symbolic coefs or file */ |
18 |
|
short clen; /* number of coefficients */ |
19 |
< |
char csym[11]; /* symbolic coefficients */ |
20 |
< |
char transpose; /* do transpose? */ |
19 |
> |
short nsf; /* number of scalars */ |
20 |
> |
short transpose; /* do transpose? */ |
21 |
|
} RUNARYOP; |
22 |
|
|
23 |
|
/* Matrix input source and requested operation(s) */ |
32 |
|
int verbose = 0; /* verbose reporting? */ |
33 |
|
|
34 |
|
/* Load matrix */ |
35 |
< |
static int |
35 |
> |
int |
36 |
|
loadmatrix(ROPMAT *rop) |
37 |
|
{ |
38 |
|
if (rop->mtx != NULL) /* already loaded? */ |
43 |
|
return(!rop->mtx ? -1 : 1); |
44 |
|
} |
45 |
|
|
46 |
+ |
extern int checksymbolic(ROPMAT *rop); |
47 |
+ |
|
48 |
+ |
/* Check/set transform based on a reference input file */ |
49 |
+ |
int |
50 |
+ |
checkreffile(ROPMAT *rop) |
51 |
+ |
{ |
52 |
+ |
static const char *curRF = NULL; |
53 |
+ |
static RMATRIX refm; |
54 |
+ |
const int nc = rop->mtx->ncomp; |
55 |
+ |
int i; |
56 |
+ |
|
57 |
+ |
if (!curRF || strcmp(rop->preop.csym, curRF)) { |
58 |
+ |
FILE *fp = fopen(rop->preop.csym, "rb"); |
59 |
+ |
if (!rmx_load_header(&refm, fp)) { |
60 |
+ |
fprintf(stderr, "%s: cannot read info header\n", |
61 |
+ |
rop->preop.csym); |
62 |
+ |
curRF = NULL; |
63 |
+ |
if (fp) fclose(fp); |
64 |
+ |
return(-1); |
65 |
+ |
} |
66 |
+ |
fclose(fp); |
67 |
+ |
curRF = rop->preop.csym; |
68 |
+ |
} |
69 |
+ |
if (refm.ncomp == 3) { |
70 |
+ |
rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB"; |
71 |
+ |
return(checksymbolic(rop)); |
72 |
+ |
} |
73 |
+ |
if (refm.ncomp == 2) { |
74 |
+ |
fprintf(stderr, "%s: cannot convert to 2 components\n", |
75 |
+ |
curRF); |
76 |
+ |
return(-1); |
77 |
+ |
} |
78 |
+ |
if (refm.ncomp == 1) { |
79 |
+ |
rop->preop.csym = "Y"; /* XXX big assumption */ |
80 |
+ |
return(checksymbolic(rop)); |
81 |
+ |
} |
82 |
+ |
if (refm.ncomp == nc && |
83 |
+ |
!memcmp(refm.wlpart, rop->mtx->wlpart, sizeof(refm.wlpart))) |
84 |
+ |
return(0); /* nothing to do */ |
85 |
+ |
|
86 |
+ |
if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) { |
87 |
+ |
fprintf(stderr, "%s: cannot resample from %d to %d components\n", |
88 |
+ |
curRF, nc, refm.ncomp); |
89 |
+ |
return(-1); |
90 |
+ |
} |
91 |
+ |
rop->preop.clen = refm.ncomp * nc; /* compute spec to ref */ |
92 |
+ |
|
93 |
+ |
for (i = 0; i < nc; i++) { |
94 |
+ |
SCOLOR scstim, scresp; |
95 |
+ |
int j; |
96 |
+ |
memset(scstim, 0, sizeof(COLORV)*nc); |
97 |
+ |
scstim[i] = 1.f; |
98 |
+ |
convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3], |
99 |
+ |
scstim, nc, rop->mtx->wlpart[0], rop->mtx->wlpart[3]); |
100 |
+ |
for (j = refm.ncomp; j-- > 0; ) |
101 |
+ |
rop->preop.cmat[j*nc + i] = scresp[j]; |
102 |
+ |
} |
103 |
+ |
memcpy(rop->mtx->wlpart, refm.wlpart, sizeof(rop->mtx->wlpart)); |
104 |
+ |
return(0); |
105 |
+ |
} |
106 |
+ |
|
107 |
|
/* Compute conversion row from spectrum to one channel of RGB */ |
108 |
< |
static void |
108 |
> |
void |
109 |
|
rgbrow(ROPMAT *rop, int r, int p) |
110 |
|
{ |
111 |
|
const int nc = rop->mtx->ncomp; |
122 |
|
} |
123 |
|
|
124 |
|
/* Compute conversion row from spectrum to one channel of XYZ */ |
125 |
< |
static void |
125 |
> |
void |
126 |
|
xyzrow(ROPMAT *rop, int r, int p) |
127 |
|
{ |
128 |
|
const int nc = rop->mtx->ncomp; |
139 |
|
} |
140 |
|
|
141 |
|
/* Use the spectral sensitivity function to compute matrix coefficients */ |
142 |
< |
static void |
143 |
< |
sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4])) |
142 |
> |
void |
143 |
> |
sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4])) |
144 |
|
{ |
145 |
|
const int nc = rop->mtx->ncomp; |
146 |
|
int i; |
147 |
|
|
148 |
|
for (i = nc; i--; ) { |
149 |
|
SCOLOR sclr; |
150 |
< |
scolorblack(sclr); |
150 |
> |
memset(sclr, 0, sizeof(COLORV)*nc); |
151 |
|
sclr[i] = 1.f; |
152 |
|
rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->mtx->wlpart); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
/* Check/set symbolic transform */ |
157 |
< |
static int |
157 |
> |
int |
158 |
|
checksymbolic(ROPMAT *rop) |
159 |
|
{ |
160 |
|
const int nc = rop->mtx->ncomp; |
161 |
|
const int dt = rop->mtx->dtype; |
162 |
+ |
double cf = 1; |
163 |
|
int i, j; |
164 |
+ |
/* check suffix => reference file */ |
165 |
+ |
if (strchr(rop->preop.csym, '.') > rop->preop.csym) |
166 |
+ |
return(checkreffile(rop)); |
167 |
|
|
168 |
|
if (nc < 3) { |
169 |
|
fprintf(stderr, "%s: -c '%s' requires at least 3 components\n", |
180 |
|
int comp = 0; |
181 |
|
switch (rop->preop.csym[j]) { |
182 |
|
case 'B': |
183 |
+ |
case 'b': |
184 |
|
++comp; |
185 |
|
/* fall through */ |
186 |
|
case 'G': |
187 |
+ |
case 'g': |
188 |
|
++comp; |
189 |
|
/* fall through */ |
190 |
|
case 'R': |
191 |
+ |
case 'r': |
192 |
+ |
if (rop->preop.csym[j] <= 'Z') |
193 |
+ |
cf = 1./WHTEFFICACY; |
194 |
|
if (dt == DTxyze) { |
195 |
|
for (i = 3; i--; ) |
196 |
< |
rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY * |
131 |
< |
xyz2rgbmat[comp][i]; |
196 |
> |
rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i]; |
197 |
|
} else if (nc == 3) |
198 |
|
rop->preop.cmat[j*nc+comp] = 1.; |
199 |
|
else |
200 |
|
rgbrow(rop, j, comp); |
201 |
|
break; |
202 |
|
case 'Z': |
203 |
+ |
case 'z': |
204 |
|
++comp; |
205 |
|
/* fall through */ |
206 |
|
case 'Y': |
207 |
+ |
case 'y': |
208 |
|
++comp; |
209 |
|
/* fall through */ |
210 |
|
case 'X': |
211 |
+ |
case 'x': |
212 |
+ |
if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze)) |
213 |
+ |
cf = WHTEFFICACY; |
214 |
|
if (dt == DTxyze) { |
215 |
|
rop->preop.cmat[j*nc+comp] = 1.; |
216 |
|
} else if (nc == 3) { |
222 |
|
else |
223 |
|
xyzrow(rop, j, comp); |
224 |
|
|
225 |
< |
for (i = nc*(dt != DTxyze); i--; ) |
226 |
< |
rop->preop.cmat[j*nc+i] *= WHTEFFICACY; |
225 |
> |
for (i = nc*(cf != 1); i--; ) |
226 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
227 |
|
break; |
228 |
|
case 'S': /* scotopic (il)luminance */ |
229 |
+ |
cf = WHTSCOTOPIC; |
230 |
+ |
/* fall through */ |
231 |
+ |
case 's': |
232 |
|
sensrow(rop, j, scolor2scotopic); |
233 |
< |
for (i = nc; i--; ) |
234 |
< |
rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC; |
233 |
> |
for (i = nc*(cf != 1); i--; ) |
234 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
235 |
|
break; |
236 |
|
case 'M': /* melanopic (il)luminance */ |
237 |
+ |
cf = WHTMELANOPIC; |
238 |
+ |
/* fall through */ |
239 |
+ |
case 'm': |
240 |
|
sensrow(rop, j, scolor2melanopic); |
241 |
< |
for (i = nc; i--; ) |
242 |
< |
rop->preop.cmat[j*nc+i] *= WHTMELANOPIC; |
241 |
> |
for (i = nc*(cf != 1); i--; ) |
242 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
243 |
|
break; |
244 |
|
case 'A': /* average component */ |
245 |
+ |
case 'a': |
246 |
|
for (i = nc; i--; ) |
247 |
|
rop->preop.cmat[j*nc+i] = 1./(double)nc; |
248 |
|
break; |
253 |
|
} |
254 |
|
} |
255 |
|
/* return recommended output type */ |
256 |
< |
if (!strcmp(rop->preop.csym, "XYZ")) { |
256 |
> |
if (!strcasecmp(rop->preop.csym, "XYZ")) { |
257 |
|
if (dt <= DTspec) |
258 |
|
return(DTxyze); |
259 |
< |
} else if (!strcmp(rop->preop.csym, "RGB")) { |
259 |
> |
} else if (!strcasecmp(rop->preop.csym, "RGB")) { |
260 |
|
if (dt <= DTspec) |
261 |
|
return(DTrgbe); |
262 |
< |
} |
186 |
< |
if ((nc > 3) & (dt <= DTspec)) |
262 |
> |
} else if (dt == DTspec) |
263 |
|
return(DTfloat); /* probably not actual spectrum */ |
264 |
|
return(0); |
265 |
|
} |
266 |
|
|
267 |
|
/* Get matrix and perform unary operations */ |
268 |
< |
static RMATRIX * |
268 |
> |
RMATRIX * |
269 |
|
loadop(ROPMAT *rop) |
270 |
|
{ |
271 |
|
int outtype = 0; |
275 |
|
if (loadmatrix(rop) < 0) /* make sure we're loaded */ |
276 |
|
return(NULL); |
277 |
|
|
278 |
< |
if (rop->preop.csym[0] && /* symbolic transform? */ |
278 |
> |
if (rop->preop.csym && /* symbolic transform? */ |
279 |
|
(outtype = checksymbolic(rop)) < 0) |
280 |
|
goto failure; |
281 |
|
if (rop->preop.clen > 0) { /* apply component transform? */ |
288 |
|
if (rop->preop.nsf == 1) { |
289 |
|
for (i = rop->preop.clen; i--; ) |
290 |
|
rop->preop.cmat[i] *= rop->preop.sca[0]; |
291 |
< |
} else if (rop->preop.nsf != rop->mtx->ncomp) { |
291 |
> |
} else if (rop->preop.nsf*rop->mtx->ncomp != rop->preop.clen) { |
292 |
|
fprintf(stderr, "%s: -s must have one or %d factors\n", |
293 |
< |
rop->inspec, rop->mtx->ncomp); |
293 |
> |
rop->inspec, |
294 |
> |
rop->preop.clen/rop->mtx->ncomp); |
295 |
|
goto failure; |
296 |
|
} else { |
297 |
< |
for (j = rop->preop.clen/rop->preop.nsf; j--; ) |
298 |
< |
for (i = rop->preop.nsf; i--; ) |
299 |
< |
rop->preop.cmat[j*rop->preop.nsf+i] *= |
300 |
< |
rop->preop.sca[i]; |
297 |
> |
for (i = rop->preop.nsf; i--; ) |
298 |
> |
for (j = rop->mtx->ncomp; j--; ) |
299 |
> |
rop->preop.cmat[i*rop->mtx->ncomp+j] |
300 |
> |
*= rop->preop.sca[i]; |
301 |
|
} |
302 |
|
} |
303 |
|
mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp, |
341 |
|
} |
342 |
|
} |
343 |
|
if (rop->preop.transpose) { /* transpose matrix? */ |
344 |
< |
mres = rmx_transpose(rop->mtx); |
268 |
< |
if (mres == NULL) { |
344 |
> |
if (!rmx_transpose(rop->mtx)) { |
345 |
|
fputs(rop->inspec, stderr); |
346 |
|
fputs(": transpose failed\n", stderr); |
347 |
|
goto failure; |
350 |
|
fputs(rop->inspec, stderr); |
351 |
|
fputs(": transposed rows and columns\n", stderr); |
352 |
|
} |
277 |
– |
rmx_free(rop->mtx); |
278 |
– |
rop->mtx = mres; |
353 |
|
} |
354 |
|
mres = rop->mtx; |
355 |
|
rop->mtx = NULL; |
362 |
|
} |
363 |
|
|
364 |
|
/* Execute binary operation, free matrix arguments and return new result */ |
365 |
< |
static RMATRIX * |
365 |
> |
RMATRIX * |
366 |
|
binaryop(const char *inspec, RMATRIX *mleft, int op, RMATRIX *mright) |
367 |
|
{ |
368 |
|
RMATRIX *mres = NULL; |
439 |
|
} |
440 |
|
|
441 |
|
/* Perform matrix operations from left to right */ |
442 |
< |
static RMATRIX * |
442 |
> |
RMATRIX * |
443 |
|
op_left2right(ROPMAT *mop) |
444 |
|
{ |
445 |
|
RMATRIX *mleft = loadop(mop); |
455 |
|
} |
456 |
|
|
457 |
|
/* Perform matrix operations from right to left */ |
458 |
< |
static RMATRIX * |
458 |
> |
RMATRIX * |
459 |
|
op_right2left(ROPMAT *mop) |
460 |
|
{ |
461 |
|
RMATRIX *mright; |
484 |
|
: (mop)->mtx->ncols) |
485 |
|
|
486 |
|
/* Should we prefer concatenating from rightmost matrix towards left? */ |
487 |
< |
static int |
487 |
> |
int |
488 |
|
prefer_right2left(ROPMAT *mop) |
489 |
|
{ |
490 |
|
int mri = 0; |
511 |
|
return(t_ncols(mop+mri) < t_nrows(mop)); |
512 |
|
} |
513 |
|
|
514 |
< |
static int |
514 |
> |
int |
515 |
|
get_factors(double da[], int n, char *av[]) |
516 |
|
{ |
517 |
|
int ac; |
521 |
|
return(ac); |
522 |
|
} |
523 |
|
|
524 |
< |
static ROPMAT * |
524 |
> |
ROPMAT * |
525 |
|
resize_moparr(ROPMAT *mop, int n2alloc) |
526 |
|
{ |
527 |
|
int nmats = 0; |
529 |
|
|
530 |
|
while (mop[nmats++].binop) |
531 |
|
; |
532 |
< |
for (i = nmats; i > n2alloc; i--) |
532 |
> |
for (i = nmats; i >= n2alloc; i--) |
533 |
|
rmx_free(mop[i].mtx); |
534 |
|
mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT)); |
535 |
|
if (mop == NULL) { |
545 |
|
int |
546 |
|
main(int argc, char *argv[]) |
547 |
|
{ |
548 |
< |
int outfmt = DTfromHeader; |
549 |
< |
int nall = 2; |
550 |
< |
ROPMAT *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT)); |
551 |
< |
int nmats = 0; |
552 |
< |
RMATRIX *mres = NULL; |
553 |
< |
int stdin_used = 0; |
554 |
< |
int i; |
548 |
> |
int outfmt = DTfromHeader; |
549 |
> |
const char *defCsym = NULL; |
550 |
> |
int nall = 2; |
551 |
> |
ROPMAT *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT)); |
552 |
> |
int nmats = 0; |
553 |
> |
RMATRIX *mres = NULL; |
554 |
> |
int stdin_used = 0; |
555 |
> |
int i; |
556 |
|
/* get options and arguments */ |
557 |
|
for (i = 1; i < argc; i++) { |
558 |
|
if (argv[i][0] && !argv[i][1] && |
575 |
|
mop[nmats].inspec = stdin_name; |
576 |
|
} else |
577 |
|
mop[nmats].inspec = argv[i]; |
578 |
+ |
if (!mop[nmats].preop.csym) |
579 |
+ |
mop[nmats].preop.csym = defCsym; |
580 |
|
if (nmats > 0 && !mop[nmats-1].binop) |
581 |
|
mop[nmats-1].binop = '.'; |
582 |
|
nmats++; |
618 |
|
goto userr; |
619 |
|
} |
620 |
|
break; |
621 |
+ |
case 'C': |
622 |
+ |
if (!n || isflt(argv[i+1])) |
623 |
+ |
goto userr; |
624 |
+ |
defCsym = mop[nmats].preop.csym = argv[++i]; |
625 |
+ |
mop[nmats].preop.clen = 0; |
626 |
+ |
break; |
627 |
|
case 'c': |
628 |
< |
if (n && isupper(argv[i+1][0])) { |
629 |
< |
strlcpy(mop[nmats].preop.csym, |
547 |
< |
argv[++i], |
548 |
< |
sizeof(mop[0].preop.csym)); |
628 |
> |
if (n && !isflt(argv[i+1])) { |
629 |
> |
mop[nmats].preop.csym = argv[++i]; |
630 |
|
mop[nmats].preop.clen = 0; |
631 |
|
break; |
632 |
|
} |
639 |
|
argv[0]); |
640 |
|
goto userr; |
641 |
|
} |
642 |
< |
mop[nmats].preop.csym[0] = '\0'; |
642 |
> |
mop[nmats].preop.csym = NULL; |
643 |
|
break; |
644 |
|
case 'r': |
645 |
|
if (argv[i][2] == 'f') |
684 |
|
else if (mres->dtype == DTxyze) |
685 |
|
outfmt = DTxyze; |
686 |
|
} |
687 |
+ |
#if DTrmx_native==DTfloat |
688 |
+ |
if (outfmt == DTdouble) |
689 |
+ |
fprintf(stderr, |
690 |
+ |
"%s: warning - writing float result as double\n", |
691 |
+ |
argv[0]); |
692 |
+ |
#endif |
693 |
|
newheader("RADIANCE", stdout); /* write result to stdout */ |
694 |
|
printargs(argc, argv, stdout); |
695 |
|
return(rmx_write(mres, outfmt, stdout) ? 0 : 1); |